How to Send Mail with attachment using Streams for reports with request page in Dynamics NAV - CloudFronts

How to Send Mail with attachment using Streams for reports with request page in Dynamics NAV

Scenario:

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.

Pre-requisites:

Microsoft Dynamics NAV 2017

Steps:

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
SMTPMailSetup.GET;

CustomerTable.RESET;
CustomerTable.SETRANGE(“No.”,Custno);
IF CustomerTable.FINDFIRST THEN BEGIN
EmailID:=CustomerTable.”E-Mail”;
CF_FTLCustomerInvoice.SETTABLEVIEW(CustomerTable);
XmlParameters:=CF_FTLCustomerInvoice.RUNREQUESTPAGE();
CustTempTable.Parameters.CREATEOUTSTREAM(OStream,TEXTENCODING::UTF8);
CustTempTable.Parameters.CREATEINSTREAM(IStream,TEXTENCODING::UTF8);
REPORT.SAVEAS(50011,XmlParameters,REPORTFORMAT::Pdf,OStream);

CLEAR(SMTPMail);
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(‘<br>’);
SMTPMail.AppendBody(‘Please find attached your Customer Invoice statement’);
SMTPMail.AppendBody(‘<HR>’);
SMTPMail.AppendBody(‘This is a system generated mail. Please do not reply to this mail!’);
SMTPMail.Send;
MESSAGE(‘Mail sent to Customer %1’,Custno);
END;

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!

Execution:

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.

Conclusion:

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


Share Story :

Secured By miniOrange