Sending Email is one of the most common features used in Dynamics AX .Though,There are many advancement when we move forward from AX2012 to D365FO .Similar is the case here we could use Predefined Framework as i have done in one of my blog(https://shayanarshi.blogspot.com/2019/10/sending-email-with-attached-excel-file.html) but there are some cases where we use just a job to send email.Below is the working code to send email in Dynamics AX
Requirements:
*Our objective it to Generate an excel File and attach it to an email and sending it to multiple
recipients
*The file data will be from financial dimension view and it will be distributed into
three worksheets that is D1,D2 and D4_5 according to conditions
*Data of Data Entity of D4 & D5 will be inserted into same worksheet that is D4_5
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class Irc_ExportDataExcelClass extends SysOperationServiceController
{
public static void main(Args _args)
{
Irc_ExportDataExcelClass exportDataExcelClass = new Irc_ExportDataExcelClass();
exportDataExcelClass.EmailSender(_args);
info('Email successfully sent');
}
Requirements:
*Our objective it to Generate an excel File and attach it to an email and sending it to multiple
recipients
*The file data will be from financial dimension view and it will be distributed into
three worksheets that is D1,D2 and D4_5 according to conditions
*Data of Data Entity of D4 & D5 will be inserted into same worksheet that is D4_5
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class Irc_ExportDataExcelClass extends SysOperationServiceController
{
public static void main(Args _args)
{
Irc_ExportDataExcelClass exportDataExcelClass = new Irc_ExportDataExcelClass();
exportDataExcelClass.EmailSender(_args);
info('Email successfully sent');
}
//Code for Email
public void EmailSender(Args _args)
{
SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();
SysMailerSMTP smtp = new SysMailerSMTP();
var user = xUserInfo::find();
var sysUser = SysUserInfo::find(user.id);
var sender = sysUser.getSendAsEmail(user);
container con = str2con(_args.parm());
Irc_ExportDataExcelClass exporter = new Irc_ExportDataExcelClass();
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
var recipient ='';
;
for(int i=1;i<=conLen(con);i++)
{
recipient = conPeek(con,i);
mailer.addTo(recipient);
}
mailer.setSubject("Financial Details");
mailer.setFrom(sender);
mailer.setBody("PFA");
memoryStream = exporter.DataLoaderExcel();
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
//The below line used to attach excel file to email.
//it will self download the file and attach it
mailer.addAttachment(memoryStream, 'D Codes FCM.xlsx', "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try
{
smtp.sendNonInteractive(mailer.getMessage());
}
catch(Exception::CLRError)
{
error(CLRInterop::getLastException().toString());
}
}
//Code For Generating Excel
public void DataLoaderExcel()
{
FINANCIALDIMENSIONVALUEENTITYVIEW financialDimension;
MemoryStream memoryStream = new MemoryStream();
using (var package = new ExcelPackage(memoryStream))
{
var currentRow = 1;
var worksheets = package.get_Workbook().get_Worksheets();
var financialDimensionWorksheet = worksheets.Add("D1");
var cells = financialDimensionWorksheet.get_Cells();
OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
System.String value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D01Project_Code'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//for D3
currentRow = 1;
worksheets = package.get_Workbook().get_Worksheets();
financialDimensionWorksheet = worksheets.Add("D3");
cells = financialDimensionWorksheet.get_Cells();
cell = cells.get_Item(currentRow, 1);
value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D03Program_Area'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//D3 work ends
//for D4_5
currentRow = 1;
worksheets = package.get_Workbook().get_Worksheets();
financialDimensionWorksheet = worksheets.Add("D4_5");
cells = financialDimensionWorksheet.get_Cells();
cell = cells.get_Item(currentRow, 1);
value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D04Location' || financialDimension.DimensionAttribute == 'D05Entity_Code'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//D4_5 work ends
package.Save();
file::SendFileToUser(memoryStream, "D_Codes_FCM");
Global::info("Data has been loaded");
}
}
}
{
SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();
SysMailerSMTP smtp = new SysMailerSMTP();
var user = xUserInfo::find();
var sysUser = SysUserInfo::find(user.id);
var sender = sysUser.getSendAsEmail(user);
container con = str2con(_args.parm());
Irc_ExportDataExcelClass exporter = new Irc_ExportDataExcelClass();
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
var recipient ='';
;
for(int i=1;i<=conLen(con);i++)
{
recipient = conPeek(con,i);
mailer.addTo(recipient);
}
mailer.setSubject("Financial Details");
mailer.setFrom(sender);
mailer.setBody("PFA");
memoryStream = exporter.DataLoaderExcel();
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
//The below line used to attach excel file to email.
//it will self download the file and attach it
mailer.addAttachment(memoryStream, 'D Codes FCM.xlsx', "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try
{
smtp.sendNonInteractive(mailer.getMessage());
}
catch(Exception::CLRError)
{
error(CLRInterop::getLastException().toString());
}
}
//Code For Generating Excel
public void DataLoaderExcel()
{
FINANCIALDIMENSIONVALUEENTITYVIEW financialDimension;
MemoryStream memoryStream = new MemoryStream();
using (var package = new ExcelPackage(memoryStream))
{
var currentRow = 1;
var worksheets = package.get_Workbook().get_Worksheets();
var financialDimensionWorksheet = worksheets.Add("D1");
var cells = financialDimensionWorksheet.get_Cells();
OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
System.String value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D01Project_Code'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//for D3
currentRow = 1;
worksheets = package.get_Workbook().get_Worksheets();
financialDimensionWorksheet = worksheets.Add("D3");
cells = financialDimensionWorksheet.get_Cells();
cell = cells.get_Item(currentRow, 1);
value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D03Program_Area'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//D3 work ends
//for D4_5
currentRow = 1;
worksheets = package.get_Workbook().get_Worksheets();
financialDimensionWorksheet = worksheets.Add("D4_5");
cells = financialDimensionWorksheet.get_Cells();
cell = cells.get_Item(currentRow, 1);
value = "Dimension Value";
cell.set_Value(value);
cell = null;
value = "Description";
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
while select financialDimension where financialDimension.DimensionAttribute == 'D04Location' || financialDimension.DimensionAttribute == 'D05Entity_Code'
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(financialDimension.DimensionValue);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(financialDimension.Description);
}
//D4_5 work ends
package.Save();
file::SendFileToUser(memoryStream, "D_Codes_FCM");
Global::info("Data has been loaded");
}
}
}
Comments
Post a Comment