How to Send Emails with CC and BCC in Business Central Using AL Language
Sending emails programmatically in Microsoft Dynamics 365 Business Central (BC) is a common requirement for customization be it sending invoices, reminders, or notifications. With the release of enhanced email capabilities in recent versions, AL developers can now include CC (Carbon Copy) and BCC (Blind Carbon Copy) recipients when sending emails.
In this blog, we’ll walk through how to send an email in Business Central using AL and how to include CC and BCC fields effectively.
Steps to Achieve goal:
Code for example.
procedure SendInvoiceEmail(var PurchaseInvHeader: Record “Purch. Inv. Header”): Boolean
var
EmailAccount: Codeunit “Email Account”;
EmailMessage: Codeunit “Email Message”;
Email: Codeunit Email;
PurchaseInvLine: Record “Purch. Inv. Line”;
Vendor: Record Vendor;
Item: Record Item;
Currency: Record Currency;
CurrencySymbol: Text;
ItemDescription: Text[2048];
ItemNumber: Code[30];
TotalAmount: Decimal;
Link: Text[2048];
EmailType: Enum “Email Recipient Type”;
begin
// Get vendor information
Vendor.SetRange(“No.”, PurchaseInvHeader.”Buy-from Vendor No.”);
if Vendor.FindFirst() then;
// Process purchase invoice lines
PurchaseInvLine.SetRange(“Document No.”, PurchaseInvHeader.”No.”);
PurchaseInvLine.SetFilter(Quantity, ‘<>%1’, 0);
if PurchaseInvLine.FindSet() then
repeat
Item.Reset();
Item.SetRange(“No.”, PurchaseInvLine.”No.”);
Item.SetRange(“Second Hand Goods”, true); // Example filter
if Item.FindFirst() then begin
ItemDescription := PurchaseInvLine.Description;
ItemNumber := PurchaseInvLine.”No.”;
TotalAmount += PurchaseInvLine.”Amount Including VAT”;
Currency.SetRange(Code, PurchaseInvHeader.”Currency Code”);
if Currency.FindFirst() then
CurrencySymbol := Currency.Symbol
else
CurrencySymbol := ‘€’; // Default symbol
end;
until PurchaseInvLine.Next() = 0;
// Generate a form link with dynamic query parameters (example only)
Link := ‘https://your-form-url.com?vendor=’ + Vendor.”No.” +
‘&invoice=’ + PurchaseInvHeader.”No.”;
// Create and configure the email
EmailMessage.Create(
Vendor.”E-Mail”,
‘Subject: Review Your Invoice’,
‘Hello ‘ + Vendor.Name + ‘,<br><br>’ +
‘Please review the invoice details for item: ‘ + ItemDescription + ‘<br>’ +
‘Total: ‘ + Format(TotalAmount) + ‘ ‘ + CurrencySymbol + ‘<br>’ +
‘Form Link: <a href=”‘ + Link + ‘”>Click here</a><br><br>’ +
‘Best regards,<br>Your Company Name’,
true // IsBodyHtml
);
// Add BCC (could also use AddCc)
EmailMessage.AddRecipient(EmailType::BCC, ‘example@yourdomain.com’);
// Update invoice status or flags (optional business logic)
PurchaseInvHeader.”Custom Status Field” := PurchaseInvHeader.”Custom Status Field”::Started;
PurchaseInvHeader.Modify();
// Send the email
exit(Email.Send(EmailMessage));
end;
To conclude, sending emails directly from AL in Business Central is a powerful way to streamline communication with vendors, customers, and internal users. By leveraging the Email Message
and Email
codeunits, developers can easily customize the subject, body, and recipients, including support for CC and BCC fields. This flexibility makes it easy to automate notifications, document sharing, or approval requests directly from your business logic.
Whether you’re integrating forms, sending invoices, or just keeping stakeholders in the loop, this approach ensures your extensions are both professional and user-friendly. With just a few lines of code, you can improve efficiency and enhance communication across your organization.
We hope you found this blog useful, and if you would like to discuss anything, you can reach out to us at transform@cloudfonts.com