Table of Contents

Attachments and inline images

A received message can carry file attachments and HTML content that references embedded images. TTMSFNCCloudGoogleGmail downloads each attachment's content individually, lets you save it locally, and can also expand an HTML body so its inline images render standalone — useful when you display a message body outside of an actual mail client, where cid: references would otherwise be broken links. This chapter covers downloading an attachment and working with inline images, both for reading and for composing.

Downloading an attachment

A message's Attachments collection is already populated with metadata (FileName, Size, MimeType, ContentID) once the message itself has been retrieved — but its content is not downloaded yet. GetAttachment downloads one attachment's data at a time into its Data property, raising OnGetAttachment. SaveToFile (or SaveToStream) then decodes that data to a local file or stream.

procedure TForm1.DownloadFirstAttachment(AMessage: TTMSFNCCloudGoogleGmailMessage);
begin
  if AMessage.Attachments.Count = 0 then
    Exit;

  TMSFNCCloudGoogleGmail1.OnGetAttachment := GmailAttachmentReady;
  TMSFNCCloudGoogleGmail1.GetAttachment(AMessage.Attachments[0]);
end;

procedure TForm1.GmailAttachmentReady(Sender: TObject;
  const AMessage: TTMSFNCCloudGoogleGmailMessage;
  const AAttachment: TTMSFNCCloudGoogleGmailMessageAttachment;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    AAttachment.SaveToFile('C:\local\' + AAttachment.FileName);
end;

Rendering a body with inline images

An HTML message that embeds images references them as cid:<ContentID>, pointing at an inline attachment's ContentID. GetBodyWithInlineImages downloads those attachments on first access and replaces each reference with the image's Base64 data, so the returned body renders correctly even outside Gmail.

function TForm1.RenderableBodyOf(AMessage: TTMSFNCCloudGoogleGmailMessage): string;
begin
  // Downloads the referenced inline attachments on first access and caches the result.
  Result := AMessage.GetBodyWithInlineImages;
end;

Combining a downloaded attachment with an inline body

Saving an attachment to disk and rendering the same message's body with its inline images expanded is the common pattern for displaying a full message, images included, in your own UI:

// Assumes a private field: FRenderableBody: string;
procedure TForm1.SaveAttachmentAndRenderBody(AMessage: TTMSFNCCloudGoogleGmailMessage);
begin
  if AMessage.Attachments.Count = 0 then
    Exit;

  TMSFNCCloudGoogleGmail1.OnGetAttachment := GmailAttachmentSavedThenRendered;
  TMSFNCCloudGoogleGmail1.GetAttachment(AMessage.Attachments[0]);
end;

procedure TForm1.GmailAttachmentSavedThenRendered(Sender: TObject;
  const AMessage: TTMSFNCCloudGoogleGmailMessage;
  const AAttachment: TTMSFNCCloudGoogleGmailMessageAttachment;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if not ARequestResult.Success then
    Exit;

  AAttachment.SaveToFile('C:\local\' + AAttachment.FileName);
  // Reuses the attachment just downloaded for any matching cid: reference.
  FRenderableBody := AMessage.GetBodyWithInlineImages;
end;

Embedding a local image when composing

ImageToBase64 encodes a local image file as Base64, optionally wrapped in an HTML <img> tag, for building an HTML message body that embeds the image directly rather than as a separate attachment.

function TForm1.BuildHTMLBodyWithLogo: string;
begin
  Result := '<p>Report attached.</p>'
    + TMSFNCCloudGoogleGmail1.ImageToBase64('C:\local\logo.png', True);
end;

Common mistakes

  • Calling SaveToFile before OnGetAttachment fires. The attachment's Data is empty until that specific attachment has been downloaded — unlike the message list, attachment content is fetched one at a time, not automatically with the message.
  • Expecting GetBodyWithInlineImages to be free of network calls. The first call downloads the referenced attachments; cache and reuse the returned string rather than calling it repeatedly for the same message.

See also