10 Jun’18

D365 Business CentralHow to Send Mail with attachment using Streams for reports with request page in Dynamics NAV


On Customer Master page, an action button is created called Email customer, on click of the action button, an email has to be sent to the respective customer with attachment of the invoices in a .pdf format. Here, the report is saves as .pdf file.

This report has a request page where Start Date and End date are entered by the user. Based on the date ranges, invoices generated for the particular user is mailed to the customer.


Microsoft Dynamics NAV 2017


1. Create a global function on the Customer master page to make a call to the codeunit. Here the Customer No. is passed as a parameter.

2. In the Function InvoiceMail, declare a parameter  CustNo. and the variables as follows:

Here CustTempTable is a Temporary table

The Code is added as follows:

InvoiceMail(Custno : Code[20]) -Function Name


SMTPMail.CreateMessage(”,SMTPMailSetup.”User ID”,EmailID,’Invoice Statement from CompanyName‘,”,TRUE);
SMTPMail.AddAttachmentStream(IStream,’Customer Invoice’+ CustomerTable.Name+’.pdf’);
SMTPMail.AppendBody(‘Hi ‘+CustomerTable.Name+’,’);

SMTPMail.AppendBody(‘Please find attached your Customer Invoice statement’);
SMTPMail.AppendBody(‘This is a system generated mail. Please do not reply to this mail!’);
MESSAGE(‘Mail sent to Customer %1’,Custno);

Explaination of the code:

1. Set the SMTL Mail setup in the Role Tailored Client. Click on Apply Office 365 Server Settings. Add the Sender email (comapny email in my case) in User ID field and password of the email id in the Password field.

2. Store the Customer Email in the Email ID variable.

3. Pass the filters applied to the CustomerTable to the report variable CF_FTLCustomerInvoice.

4. Run the request page. The Run request page passes the request parameters in an xml format which is stored in a text variable XmlParameters.

5. Create an OutStream to save the the XmlParamters received in a File Parameters in a .xml format.

6. Create Instream to read the .xml file.

7. REPORT.SAVEAS(50011,XmlParameters,REPORTFORMAT::Pdf,OStream); saves the xmlparameters from the outsream in an pdf format.

8. Use SMTPMail.CreateMessage function to enter the sender’s and receipient’s  email, Subject  etc.

9. Add the attachement from the InputStream.

10. Draft a body of the email and Use SMTPMail.Send to Send the email. Yay.Email is Sent!


1.Click on Action button, Request page opens.

2. Enter the Start date and End date and click on OK.

3. An email is sent to the customer.


Overview of the blog, first setup the SMTL Mail setup. Create an action button and create a function call. In the function defination, code as above and email is sent

Written by

Chris D'Silva

Software Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • “$ undefined error” conflict issue in Dynamics 365 v9.0

    18 June’ 2018

    Introduction: After upgrade of dynamics CRM 8.2 to 9.0 jQuery start breaking or you will receive $ undefined error. Th...

    Read more
  • Creating Custom Fields

    18 June’ 2018

    Introduction: This blog articles explains the ability to add custom fields is available in platform update 13 and later...

    Read more
  • Explore Business Central API through Postman using Basic Authentication

    14 June’ 2018

    Introduction: In this article, we will walk through steps on how to authenticate Business Central API using Basic Authe...

    Read more
  • Setup Positive Pay Export

    14 June’ 2018

    Introduction: What is Positive Pay Export and Why it’s used? Positive Pay Export is a file that is given to the ...

    Read more
  • Create Approval Flow with D365 conection

    12 June’ 2018

    Introduction: This blog explains how to Create Approval Flow using Microsoft Flows.  Use Case: When work order is upd...

    Read more