emailaxaptaemail-attachmentsx++dynamics-ax-2012

How to create an Excel file and it by Email, using code x++ in Dynamics AX2012?


I need to create an Excel file and to send it by Email, by X++ code in to Dyncamics AX2012. If it's possible I don't want to save the data in a physical path/folder.

I wrote this code, but after I need to send the Excel.

I would like to follow the D365 instructions (https://axexplorer.wordpress.com/2017/07/18/create-an-excel-file-and-send-it-through-e-mail-in-d365-for-operationax-7/ )in Dynamics AX 2012 (In AX2012 some command are not available)

SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject,body;
InteropPermission interopPermission;
SysMailerMessageBuilder  mailer;
SysMailerAttachments attach;
System.Exception e;

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell cell;

int row;
str contact;
int recordscount;
MYTABLE myTable;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
row = 1 ;

mailer.subject("MY subject");
mailer.fromAddress("MY_from@com");
mailer.htmlBody("MY_BODY");
mailer.tos().appendAddress("MY_to.com");

application = SysExcelApplication::construct();

workbooks = application.workbooks();
workbook = workbooks.add();
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
//cells.range(‘A:A’).numberFormat(‘@’);
cell = cells.item(1,1);
cell.value("Field_I");
cell = cells.item(1,2);
cell.value("Field_II");

while select myTable
{
    row++;

    cell = cells.item(row, 1);
    cell.value(myTable.Field_I);
    cell = cells.item(row, 2);
    cell.value(myTable.Field_I);
}
application.visible(true);
application.save();
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

I'm not able to save the file in temporary stream.

In AX2012 are not available the

SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();;
var package = new OfficeOpenXml.ExcelPackage(memoryStream)

//The below line used to attach excel file to email. mailer.addAttachment(memoryStream, ‘MYFILE.xlsx’, ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);

There is any way in Dynamics AX2012 to create an excel file and send the file to email (withouth saving in to the path).

Thanks in advance.


Solution

  • Following the @Alex Kwitny kind comment, I used this code:

    #Properties
    #AOT
    #File
    #define.io_rw("rw")
    Filename                  filename;
    str                       tempPath;
    FileIOPermission          fileIOPermission;
    
    if(_runOnService)
    {
        fileIOPermission = new FileIOPermission('','r');
        fileIOPermission.assert();
    
        tempPath = WinAPIServer::getTempPath(); 
    
        CodeAccessPermission::revertAssert();
        filename = tempPath + _name + #xml;
    }
    else
    {
       tempPath = WinAPI::getTempPath();
    
       filename = tempPath + _name + #xml;
    }
    

    tempPath = WinAPI::getTempPath();

    tempPath = WinAPIServer::getTempPath();

    Thanks all.