Table of Contents

Attachments and inline images

A received message can carry file attachments and HTML content that references embedded images. TTMSFNCCloudMicrosoftOutlookMail downloads attachment content, 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 attachments and working with inline images, both for reading and for composing.

Downloading attachments

GetAttachments requests the attachments of a message, raising OnGetAttachments when they are available on the message's Attachments collection. Each TTMSFNCCloudMicrosoftOutlookMailAttachment carries its content Base64-encoded; SaveToFile (or SaveToStream) decodes it to a local file or stream.

procedure TForm1.DownloadAttachments(AMail: TTMSFNCCloudMicrosoftOutlookMailItem);
begin
  TMSFNCCloudMicrosoftOutlookMail1.OnGetAttachments := OutlookMailAttachmentsReady;
  TMSFNCCloudMicrosoftOutlookMail1.GetAttachments(AMail);
end;

procedure TForm1.OutlookMailAttachmentsReady(Sender: TObject;
  const AMail: TTMSFNCCloudMicrosoftOutlookMailItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success and (AMail.Attachments.Count > 0) then
    AMail.Attachments[0].SaveToFile('C:\local\' + AMail.Attachments[0].Name);
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 Outlook.

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

Combining downloaded attachments with an inline body

Requesting the attachments and the inline-expanded body together is the common pattern for displaying a full message, images included, in your own UI:

procedure TForm1.DisplayFullMessage(AMail: TTMSFNCCloudMicrosoftOutlookMailItem);
begin
  TMSFNCCloudMicrosoftOutlookMail1.OnGetAttachments := OutlookMailReadyToDisplay;
  TMSFNCCloudMicrosoftOutlookMail1.GetAttachments(AMail);
end;

procedure TForm1.OutlookMailReadyToDisplay(Sender: TObject;
  const AMail: TTMSFNCCloudMicrosoftOutlookMailItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if not ARequestResult.Success then
    Exit;

  // Attachments are now downloaded; expanding the body reuses them for
  // any inline cid: references instead of downloading them again.
  FRenderableBody := AMail.GetBodyWithInlineImages; // assumes a private field: FRenderableBody: string;
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>'
    + TMSFNCCloudMicrosoftOutlookMail1.ImageToBase64('C:\local\logo.png', True);
end;

Common mistakes

  • Calling SaveToFile before OnGetAttachments fires. The attachment's content is empty until the attachments have actually been downloaded.
  • 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