Skip to content
Extracting email attachments in Business Central

Let's catch the E-Mail Attachments

Daniel Gorski
Daniel Gorski October 26, 2022
3 min read

Have you ever tried to get the attachments of an E-Mail which have been sent inside of Business Central?

The Main Question is: Do you want to store all Sales Quotes which have been sent to your customers/partners? Your employees are sending many mails with different versions of Quotes and a centralized storage would be nice?

Our goal is to download or even upload the body of our mail and all attachments to Sharepoint Online by using our Beyond Cloud Connector (which can be extended by any developer).

Let's step back and understand the implementation:

I am using Business Central v21 and Codeunit "Mail Management" is full of Mailing procedures to make the "Big Thing" run.

For our scenario there is an Event, which is very helpful:

OnSendViaEmailModuleOnAfterEmailSend(Message, TempEmailItem, MailSent, Cancelled, HideEmailSendingError);

Message is declared as Codeunit "Email Message" and holds the Body of your Mail and all Attachments.

GetBody() returns a String, so we need to use an InStream to download or upload it later:

if Message.GetBody() <> '' then begin
    Attachment.CreateOutStream(OutStr, TextEncoding::UTF8);
    OutStr.Write(Message.GetBody());
    Attachment.CreateInStream(BodyInStream, TextEncoding::UTF8);
    UploadToBeyondCloudConnectorOrDownload(BodyInStream, 'Mail.html', CloudStorageSalesHeader, SalesHeader);
end;

Let's iterate also through all Attachments:

if Message.Attachments_First() then
repeat
    Message.Attachments_GetContent(AttachmentStream);
    UploadToBeyondCloudConnectorOrDownload(AttachmentStream, Message.Attachments_GetName(), CloudStorageSalesHeader, SalesHeader);
until Message.Attachments_Next() = 0;

TempEmailItem is declared as Record "Email Item" and is aware of some background information. Like the the Source Tables, System IDs and Documents:

TempEmailItem.GetSourceDocuments(SourceTableIDs, SourceIDs, SourceRelationTypes);

We need to check if this Mail has been sent from our Sales Quote, right?

By returning a List, we need to get the first entry:

if SourceTableIDs.Get(1) <> Database::"Sales Header" then
    exit;

if SourceRelationTypes.Get(1) <> Enum::"Sales Document Type"::Quote.AsInteger() then
    exit;

if not SalesHeader.GetBySystemId(SourceIDs.Get(1)) then
    exit;

And then let's download or upload our InStream:

local procedure UploadToBeyondCloudConnectorOrDownload

The User should answer that question by using a StrMenu:

local procedure UploadToBeyondCloudConnectorOrDownload(
    var AttachmentInStream: InStream;
    Filename: Text;
    CloudStorageSalesHeader: Record "BYD Cloud Storage";
    SalesHeader: Record "Sales Header")

case selected of
1:
    OK := DownloadFromStream(AttachmentInStream, 'Download', '', '', NewFileName);
2:
begin
    Clear(CloudStorageMgt);
    CloudStorageMgt.PerformChunkedFileUpload(
        AttachmentInStream,
        CloudStorageSalesHeader,
        SalesHeader.RecordId(),
        SalesHeader.TableCaption(),
        CloudStorageMgt.GetPrimaryKeyText(SalesHeader.RecordId()),
        NewFileName,
        FileManagement.GetFileNameMimeType(NewFileName));
end;
end;

And let's add a timestamp to all attachments which will work as a "Version":

FileManagement: Codeunit "File Management";
TypeHelper: Codeunit "Type Helper";

TodayFormatted := TypeHelper.GetFormattedCurrentDateTimeInUserTimeZone(DateTimeFormatLbl);
NewFileName := StrSubstNo(
    FileNameLbl,
    FileManagement.GetFileNameWithoutExtension(FileName),
    TodayFormatted,
    FileManagement.GetExtension(FileName));

Let's give it a try and see how it works:

Happy coding :)!

The app for this topic

Beyond Cloud Connector Logo

Beyond Cloud Connector

Business Central SharePoint integration, plus Azure Files and Blob Storage: offload documents, cut database bloat, access files from anywhere.

Beyond Cloud Connector

Related posts

▸ All posts
Merge function in Business Central

How-To Merge PDF Files in Business Central?

March 24, 2021 · Daniel Gorski

Learn how to merge PDF files directly in Dynamics 365 Business Central using DropZone, Azure Files, and SharePoint Online.

Read More
Read More: How-To Merge PDF Files in Business Central?
Fixing DNS issues in Docker Containers

Docker DNS Not Working in Windows Containers

June 13, 2025 · Malte Petersen

Docker container can't resolve host names? Set a DNS server inside the Windows container, or once for all containers in daemon.json. Two fixes.

Read More
Read More: Fixing DNS Issues in Windows-Based Docker Containers for Business Central Testing
Transfer user personalization in Business Central

Transfer User Personalization?

March 11, 2025 · Daniel Gorski

Spent hours personalizing your Business Central sandbox? Export and import user personalization to keep UI customizations across environments.

Read More
Read More: Transfer User Personalization?