Today, I will be sharing the code snippet which can utilized to send email along with attachment in a batch job in AX 2009.
We will be using standard mail mechanism of the system and will utilize SysOutgoingEmailTable and SysOutgoingEmailData tables.
Following are perquisites for the this process:
- Email parameters filled in : Administration > Administration Area > Setup > E-mail parameters.
- Email distributor batch must be running : Administration > Administration Area > Periodic > E-mail processing > Batch.
- A class which extends RunBaseBatch and add a method in it which will have below code snippet.
void sendEmail()
{
SysOutgoingEmailTable outgoingEmailTable;
SysEmailItemId nextEmailItemId;
Map map;
str SenderName, SenderEmail, To, Subject, Body;
SysOutgoingEmailData outgoingEmailData;
FileIOPermission fileIOPermission;
InteropPermission interopPermission;
BinData binData;
FileName attachmentFileName;
SysEmailParameters emailParams;
int maxAttachmentSize;
str tmpPath;
str filePath;
str fileName;
str fileExtension;
container attachmentData;
;
try
{
SenderName = "Tester";
SenderEmail = "test@gmail.com";
To = "test@gmail.com";
Subject = "Subject for test ";
Body = "test email";
//Email parameters
emailParams = SysEmailParameters::find();
maxAttachmentSize = emailParams.MaxEmailAttachmentSize;
ttsbegin;
nextEmailItemId = EventInbox::nextEventId();
outgoingEmailTable.EmailItemId = nextEmailItemId;
outgoingEmailTable.IsSystemEmail = NoYes::No;
outgoingEmailTable.Sender = SenderEmail;
outgoingEmailTable.SenderName = SenderName;
outgoingEmailTable.Recipient = To;
outgoingEmailTable.Subject = SysEmailMessage::stringExpand(Subject, map);
outgoingEmailTable.Priority = eMailPriority::Normal ;
outgoingEmailTable.WithRetries = false;
outgoingEmailTable.RetryNum = 0;
outgoingEmailTable.UserId = curUserId();
outgoingEmailTable.Status = SysEmailStatus::Unsent;
outgoingEmailTable.Message = Body;
outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();
outgoingEmailTable.insert();
attachmentFileName = 'D:\\test.xlsx';
fileIOPermission = new FileIOPermission(attachmentfileName,'r');
fileIOPermission.assert();
if(WinAPIServer::fileExists(attachmentfileName) && (WinAPIServer::fileSize(attachmentfileName) < (maxAttachmentSize * 1000000)))
{
binData = new BinData();
binData.loadFile(attachmentfileName);
attachmentData = binData.getData();
}
CodeAccessPermission::revertAssert();
//Add attachment record
outgoingEmailData.EmailItemId = nextEmailItemId;
outgoingEmailData.DataId = 1;
outgoingEmailData.EmailDataType = SysEmailDataType::Attachment;
outgoingEmailData.Data = attachmentData;
[filePath, filename, fileExtension] = Global::fileNameSplit(attachmentfileName);
outgoingEmailData.FileName = filename;
outgoingEmailData.FileExtension = fileExtension;
outgoingEmailData.insert();
ttscommit;
}
catch
{
throw error("Failed to send email");
}
}
Output :
Check your batch job status in the Basic > Basic Area > Common Forms > Batch job list- User.
Check your email status : Administration > Administration Area > Periodic > E-mail processing > E-mail sending status.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.