Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

How to create an Excel file and send it by Email, using code x++

Posted on by

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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,989 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans