Hi NithinPeddi,
Have a look at the method emailReport() from the SrsReportRunMailer class, as you can see in the following piece of code, this is the out-of-the-box method to add attachments and send emails:
public boolean emailReport(SrsReportEMailDataContract emailContract, System.Byte[] reportBytes, str fileName)
{
SRSReportFileFormat fileFormat;
boolean result = false;
// Check args and validate contract
if(!emailContract)
{
throw error(strfmt("@SYS318601", 'emailContract'));
}
if(reportBytes.Length == 0)
{
throw error(strfmt("@SYS318601", 'reportBytes'));
}
if(!fileName)
{
throw error(strfmt("@SYS318601", 'fileName'));
}
emailContract.validate();
// Using mailer to send out report non interactively.
if(mailer == null)
{
this.initMailer();
}
if (mailer)
{
// Construct mailer message builder and pass all the values from the contract.
var messageBuilder = new SysMailerMessageBuilder();
messageBuilder.setFrom(fromAddress)
.addTo(emailContract.parmTo())
.addCc(emailContract.parmCc())
.setSubject(emailContract.parmSubject())
.setBody(emailContract.parmBody())
.addAttachment(new System.IO.MemoryStream(reportBytes), fileName);
result = mailer.sendNonInteractive(messageBuilder.getMessage());
}
return result;
}
You could extend the SrsReportRunMailer class and get your file as a Memory stream and send the stream as a attachment.
System.Byte[] binData1;
System.IO.Stream stream1;
// Turn the Bytes into a stream
for(int i = 0; i < conLen(binData); i )
{
binData1 = conPeek(binData,i 1);
stream1 = new System.IO.MemoryStream(binData1);
}
var messageBuilder = new SysMailerMessageBuilder();
messageBuilder.addAttachment(stream1,'abcd.pdf');
Refer to the following blog:
https://allaboutmsdynamics.wordpress.com/2018/08/10/d365-ax7send-email-through-x-code/
Hope this helps.