Hello Devs!
Sending auto email functionality is one of the most common functionality that client asks for. Similarly, I have documented this blog to help people to achieve this functionality.
Pre Requisites for this approach:
1) SMTP setting must be configured based on server.
Core Logic :
public void sendEmailwithAttachment(SalesTable _salesTable)
{
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
SysOperationQueryDataContractInfo sysOperationQueryDataContractInfo;
SrsReportRunController reportRunController;
SRSReportExecutionInfo reportExecutionInfo;
SRSPrintDestinationSettings printDestinationSettings;
SRSReportRunService srsReportRunService;
SRSProxy srsProxy;
Object dataContractInfoObject;
Map reportParametersMap;
System.Byte[] reportBytes;
Filename fileName;
Args args;
System.IO.MemoryStream memoryStream;
System.IO.MemoryStream fileStream;
Email toEmail ;
CfzEcomDigiReceiptContract contract;
Map templateTokens;
str emailSubject, emailBody;
CustTable custtable;
SalesTable SalesTable;
logisticsPostalAddress logisticsPostalAddress;
custtable = custtable::find(_salesTable.CustAccount);
reportRunController = new SrsReportRunController();
contract = new CfzEcomDigiReceiptContract();
reportExecutionInfo = new SRSReportExecutionInfo();
srsReportRunService = new SrsReportRunService();
reportBytes = new System.Byte[0]();
args = new Args();
templateTokens = new Map(Types::String, Types::String);
var messageBuilder = new SysMailerMessageBuilder();
contract.parmSalesId(_salesTable.SalesId);
fileName = strFmt("@CFZLabels:fileName", custTable.AccountNum);
reportRunController.parmArgs(args);
reportRunController.parmReportName(ssrsReportStr(CfzEcomDigiReceipt, Report));
reportRunController.parmShowDialog(false);
reportRunController.parmLoadFromSysLastValue(false);
reportRunController.parmReportContract().parmRdpContract(contract);
printDestinationSettings = reportRunController.parmReportContract().parmPrintSettings();
printDestinationSettings.printMediumType(SRSPrintMediumType::File);
printDestinationSettings.fileName(fileName);
printDestinationSettings.fileFormat(SRSReportFileFormat::PDF);
reportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
reportRunController.parmReportContract().parmReportExecutionInfo(reportExecutionInfo);
srsReportRunService.getReportDataContract(reportRunController.parmreportcontract().parmReportName());
srsReportRunService.preRunReport(reportRunController.parmreportcontract());
reportParametersMap = srsReportRunService.createParamMapFromContract(reportRunController.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
srsProxy = SRSProxy::constructWithConfiguration(reportRunController.parmReportContract().parmReportServerConfig());
reportBytes = srsproxy.renderReportToByteArray(reportRunController.parmreportcontract().parmreportpath(),
parameterValueArray,
printDestinationSettings.fileFormat(),
printDestinationSettings.deviceinfo());
memoryStream = new System.IO.MemoryStream(reportBytes);
memoryStream.Position = 0;
fileStream = memoryStream;
toEmail = custtable.email();
if ( toEmail)
{
SysEmailTable SysEmailTable;
select firstonly SenderAddr, EmailId from SysEmailTable;
emailSubject = strFmt("@CFZLabels:ECOReceipt", _salesTable.CustomerRef);
templateTokens.insert("@SYS74341", emailSubject);
templateTokens.insert("@SYS4009003", filename);
logisticsPostalAddress = LogisticsPostalAddress::findRecId(_salesTable.DELIVERYPOSTALADDRESS);
emailBody = strFmt("@CFZLabels:EcoBody", _salesTable.CustomerRef, logisticsPostalAddress.address);
emailBody = SysEmailMessage::stringExpand(emailBody, SysEmailTable::htmlEncodeParameters(templateTokens));
emailBody = strReplace(emailBody, '\n', '<br>');
messageBuilder.addTo(toEmail)
.setSubject(emailSubject)
.setBody(emailBody)
.addCC("");
messageBuilder.setFrom(SysEmailTable.SenderAddr , SysEmailTable.SenderName) ;
messageBuilder.addAttachment(fileStream, fileName);
SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());
info(strFmt("@XYZLabels:EmailSentMessage", custtable.AccountNum));
select forupdate salestable
where salestable.salesid == _salesTable.SalesId;
ttsbegin;
salestable.XYZIsEmailSent = NoYes::Yes;
salestable.update();
ttscommit;
}
else
{
info(strFmt("@XYZLabels:NoEmail", custtable.AccountNum));
}
}
Comments
Post a Comment