Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Attachment along with an Email

Posted on by 281

Dear Members,

 I would like to know how to make an attachment along with an email. For example, I would like to send aged accounts payable report once it is generated. 

Please provide me an appropriate solution.

Thanks and Regards,

Saravanan M

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,696 Super User 2024 Season 2 on at
    RE: Attachment along with an Email

    Hi, hope the following helps.

    Table ‘SMTP Mail Setup’ and Codeunit ‘SMTP Mail’ is marked for removal Tag: 17.0

    https://yzhums.com/7485/

    Thanks.

    ZHU

  • pankaj.k Profile Picture
    pankaj.k 962 on at
    RE: Attachment along with an Email

    I use Email Message. you can check my code also.

  • M Saravanan Profile Picture
    M Saravanan 281 on at
    RE: Attachment along with an Email

    Dear YUN ZHU,

    Thank you so much for your reply. We followed one of your blog to meet our requirement. Unfortunately we are not able to meet our exact requirement as in Business Central the  SMTP Mail setup(Record 409) and SMTP Mail (codeunit 400) had been removed and replaced by Email Message (replaces SMTP Mail) and Email Accounts (replaces SMTP Mail setup).

    We are getting stuck at this point. Please let us know an alternative solution to meet our requirement.

    The Blog which we followed earlier:   https://yzhums.com/1799/

    Thanks in Advance,

    M Saravanan

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,696 Super User 2024 Season 2 on at
    RE: Attachment along with an Email

    Hi, You can try out the following latest new feature.

    Use default attachments in email sent from Dynamics 365 Business Central

    https://yzhums.com/32793/

    Extend Email Capabilities:

    https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-extending-email#email-scenarios

    https://redandbundle.com/2022/04/19/create-a-custom-email-scenario-for-business-central/

    Hope this helps as well.

    Thanks.
    ZHU

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
  • Suggested answer
    pankaj.k Profile Picture
    pankaj.k 962 on at
    RE: Attachment along with an Email

    Hello M Saravanan,


    Please find the below code that will help you how to create mail and attached report.


    procedure SendPaperStatementMail(Var Rec_AccountNumber: Text)
    var
    email: Codeunit Email;
    emailMsg: Codeunit "Email Message";
    tempblobcu: Codeunit "Temp Blob";
    cnv64: Codeunit "Base64 Convert";
    InStr: InStream;
    txtB64: Text;
    Text001: Text;
    RecRef: RecordRef;
    Outstreamreport: OutStream;
    LoanStatement: Report "Loan Statement Report";
    LoanHeader: Record Ecl_LoanHeader;
    char13: Char;
    char10: Char;
    NewLine: Text;
    cominfo: Record "Company Information";
    custrec: Record customer;
    SalesRec: Record "Sales & Receivables Setup";
    begin
    cominfo.Get();
    SalesRec.Get();
    LoanHeader.Reset();
    LoanHeader.SetRange(AccountNumber, Rec_AccountNumber);
    IF LoanHeader.FindFirst() then
    if SalesRec.PaperStatementEmail <> '' then
    if custrec.get(LoanHeader."Customer No.") then
    if custrec.ecl_SendPaperStatement then
    if (LoanHeader."Statement Date" <> 0D) or (LoanHeader."Statement No" <> 0) then begin
    RecRef.GetTable(LoanHeader);
    tempblobcu.CreateOutStream(Outstreamreport);
    Report.SaveAs(50191, 'Test', ReportFormat::Pdf, Outstreamreport, RecRef);
    char13 := 13;
    char10 := 10;
    NewLine := char10;
    Text001 := StrSubstNo('Dear %1,', LoanHeader.name);
    Text001 += NewLine + NewLine;
    Text001 += StrSubstNo('Please find attached your monthly statement for your ggg Account %1 which has a current balance of $%2.', LoanHeader.AccountNumber, format(ROUND(LoanHeader.LastClosingBalance, 0.01, '='), 0, '<Precision,2:2><Standard Format,0>'));
    Text001 += NewLine + NewLine;
    Text001 += StrSubstNo('The minimum payment required for this month is $%1 and it is due on %2.', format(ROUND(LoanHeader.Minimum_Payment, 0.01, '=') + LoanHeader.Total_Arrears, 0, '<Precision,2:2><Standard Format,0>'), Format(LoanHeader.NextDueDate, 0, '<Day,2>/<Month,2>/<Year4>'));
    Text001 += NewLine + NewLine;
    Text001 += StrSubstNo('Please contact us on %1 if you have any questions or would like to speak to us about your account.', cominfo."Phone No. 2");
    Text001 += NewLine + NewLine;
    Text001 += NewLine + NewLine;
    Text001 += 'Sincerely';
    Text001 += NewLine + NewLine;
    Text001 += 'The ggTeam' + NewLine;
    Text001 += cominfo."Home Page" + NewLine;
    text001 += cominfo."Phone No. 2";
    tempblobcu.CreateInStream(InStr);
    txtB64 := cnv64.ToBase64(InStr, true);
    emailMsg.Create(SalesRec.PaperStatementEmail, StrSubstNo('Paper Statement for the Loan Account No. %1', LoanHeader.AccountNumber), Text001);
    emailMsg.AddAttachment('Statement for ' + LoanHeader.name + ' as of ' + Format(LoanHeader."Statement Date", 0, '<Day,2>-<Month,2>-<Year>') + '.pdf', 'application/pdf', txtB64);
    IF email.Send(emailMsg) then;
    end;
    end;

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Attachment along with an Email

    Hi,

    You can try below


    EmailOutStream: OutStream;
    RecRef: RecordRef;
    email: Codeunit Email;
    EmailMessage: Codeunit "Email Message";


    RecRef.GetTable('your report table');
    RecRef.SetTable('your report table');

    TempBlob.CreateInStream(EmailInStream, TextEncoding::UTF8);
    Tempblob.CREATEOUTSTREAM(EmailOutStream, TextEncoding::UTF8);
    REPORT.SaveAs('report id, '', ReportFormat::Pdf, EmailOutStream, RecRef);

    EmailMessage.Create('To email id', 'your subject', 'email body');
    EmailMessage.AddAttachment('file name', 'PDF', EmailOutStream);
    email.Send(EmailMessage, Enum::"Email Scenario"::Default);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans