How to send SMTP mail from Dynamics 365 Business Central

Dynamics 365 Business Central

Hello everyone.
This time we will discuss how to send SMTP mail from Dynamics 365 Business Central.
In order to explain this function, I assumed there is a customer request.
“After the sales order processor posts the sales order, the business manager needs to automatically receive the notification email from Business Central. Some order information must be included in the email.”

OK, this doesn’t sound very difficult. But if we don’t know how, it feels like there’s no way to do it.

Let’s start.
First we have to setup the SMTP mail in Business Central.

It is recommended to use “Apply office …Server Settings”.

After setting, you can run “Test Email Setup”.

Completed.

Then we must find the event in which the sending mail encoding can be run.
Event OnAfterDeleteAfterPostingin Codeunit Sales-Post.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterDeleteAfterPosting', '', false, false)]

local procedure OnAfterDeleteAfterPosting(SalesHeader: Record "Sales Header"; SalesInvoiceHeader: Record "Sales Invoice Header"; SalesCrMemoHeader: Record "Sales Cr.Memo Header"; CommitIsSuppressed: Boolean)
var
begin
end;

Create a Codeunit to subscribe to this event.

The core program for sending SMTP mail is Codeunit 400 “SMTP Mail”.

CreateMessage function is most important.

OK, let’s check the complete code is as follows directly.

codeunit 50101 SendEmailAuto
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterDeleteAfterPosting', '', false, false)]
    local procedure OnAfterDeleteAfterPosting(SalesHeader: Record "Sales Header"; SalesInvoiceHeader: Record "Sales Invoice Header"; SalesCrMemoHeader: Record "Sales Cr.Memo Header"; CommitIsSuppressed: Boolean)
    var
        SmtpMailSetup: Record "SMTP Mail Setup";
        Mail: Codeunit "SMTP Mail";
        Recipients: List of [Text];
        Subject: Text;
        Body: Text;
        SalesPostedTitle: Label 'The Sales Document %2 of Customer %1 has been posted.';
        SalesPostedMsg: Label 'Dear Manager<br><br>The Sales Document <font color="red"><strong>%2</strong></font> of Customer <strong>%1</strong> has been posted.<br> The total amount is <strong>%3</strong>. <br>The Posted Invoice Number is <strong>%4</strong>. <br> User ID <strong>%5</strong>';
    begin
        if not SmtpMailSetup.Get() then
            exit;
        Recipients.Add('yzhums@outlook.jp');
        Recipients.Add('admin@CRMbc565011.onmicrosoft.com');
        Recipients.Add('admin@CRMbc350034.onmicrosoft.com');
        SalesInvoiceHeader.CalcFields("Amount Including VAT");
        Subject := StrSubstNo(SalesPostedTitle, SalesHeader."Sell-to Customer Name", SalesHeader."No.");
        Body := StrSubstNo(SalesPostedMsg, SalesHeader."Sell-to Customer Name", SalesHeader."No.", SalesInvoiceHeader."Amount Including VAT", SalesInvoiceHeader."No.", UserId);
        Mail.CreateMessage('Business Central', SmtpMailSetup."User ID", Recipients, Subject, Body, true);
        if not Mail.Send() then
            Message(Mail.GetLastSendMailErrorText());
    end;
}

Program Flow:
1. Check SMTP Mail Setup
2. Add Recipients (You can also create the Recipients fields on the setup page)
3. Calcfields for flowfield.
4. Set mail subject.
5. Set mail body.
6. Create mail.
7. Send mail
7.1 If send failed, get last send mail error.

Note:
The body of the email is in html format by default, so html tags can be used here.

After finishing, let’s publish it to Business Central.

Create a new Sales Order and post it.

Completed

Check the mail.

This blog mainly discussed one of the situations. It may be more complicated in actual development.
So about sending SMTP mails, just remember two things:

1.Setup the SMTP mail in Business Central.
2.Use the functions available in Codeunit Sales-Post

Update 2021/01
After BC17, you need to use the method in the following blog.
Dynamics 365 Business Central: Table ‘SMTP Mail Setup’ and Codeunit ‘SMTP Mail’ is marked for removal Tag: 17.0

Hope this will help.
Thanks.

コメント

タイトルとURLをコピーしました