mercoledì 14 dicembre 2022

D365FFO – Send email and attachment by code

Following these steps it possible to create an Excel file from code, and send it by email as an attachment without  saving it in a physically folder.

From Cloud and OnPrem environment.

Example x++ code:

 using OfficeOpenXml.Style;   
 using OfficeOpenXml.Table;   
 using System.Net;    
 using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;   
 using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;    
 using Microsoft.Dynamics.ApplicationPlatform.Environment;    
 using Microsoft.Dynamics.AX.Framework.FileManagement;   
    
 public void createExcelandSendByEmail()   
 {   
      #Properties   
      #AOT   
      #File   
   
      str emailSenderName;   
      str emailSenderAddr;   
      str emailSubject;   
      str emailBody;   
   
      emailSubject      = "to set";   
      emailBody           = "to set";   
      emailSenderAddr = "to set";   
      emailSenderName = "to set";    
      System.IO.Stream workbookStream = new System.IO.MemoryStream();   
      System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();    
   
      using(var package = new OfficeOpenXml.ExcelPackage(memoryStream))   
      {    
           var worksheets = package.get_Workbook().get_Worksheets();   
           var worksheet = worksheets.Add("Sheet1");   
           var cells  = worksheet.get_Cells();       
           var currentRow = 1;    
   
           /*-------HEADER PART -START- -------*/    
   
           var cell = cells.get_Item(currentRow,1);    
           cell.set_Value("First Value");    
           cell=null;   
   
           cell = cells.get_Item(currentRow,2);   
           cell.set_Value("Second Value");    
           cell=null;   
   
           /*-------HEADER PART -END- -------*/    
   
           /*-------RECORD -START- -------*/    
   
           currentRow++; //    
   
           cell = null;  
   
           cell= cells.get_Item(currentRow, 1);   
           cell.set_Value("Value");   
   
           cell= null;   
   
           cell= cells.get_Item(currentRow, 2);   
           cell.set_Value("Value");   
   
           /*-------RECORD -END- -------*/   
   
           package.Save();   
   
           var messageBuilder = new SysMailerMessageBuilder();   
             
           messageBuilder.addTo(listRecipient);   
           messageBuilder.setSubject(emailSubject);   
           messageBuilder.setBody(emailBody);   
           messageBuilder.Setpriority("add your value");   
             
           messageBuilder.addAttachment(memoryStream, "Excel_File_Name.xlsx"); // Attach the Excel file    
           SysMailerFactory::sendNonInteractive(messageBuilder.getMessage()); // Send email available in batch    
      }     
 }
Parameters:

smtp.office365.com
587
SSL/TLS required a YES




Set the Outlook address in the execution user setup ( the user who will perform the process).
SMTP provider Id.


enjoy