Sending custom email notifications is a very common requirement for Dynamics AX. The code sample included in this post contains the following features: attaching files, designating email priority setting, specifying To/From/CC recipients, and populating the subject line and body of the email. It’s assumed that you already have an operational SMTP server which is accessible from within your local network.
Configuration
Before email can be sent using SMTP, you’ll need to specify the SMTP server settings in the system administration module of AX.
Dynamics AX 2012:
Dynamics AX 2009:
The field values that must be specified in this form will depend on how the SMTP server is configured, but many times only the first two fields need to be set. Once you populate this form, you’ll want to send a test email.
Code sample
The following code may be copied into a new job in the AOT. Set the variables at the top according to your needs, and execute the job.
static void TestEmail(Args _args) { // Set these variables. str sender = 'sender@somedomain.com'; str recipient = 'recipient1@somedomain.com; recipient2@somedomain.com'; str cc = 'cc1@somedomain.com; cc2@somedomain.com'; str subject = 'Test'; str body = 'Test'; str fileName = @'C:\test.txt'; List toList; List ccList; ListEnumerator le; Set permissionSet; System.Exception e; str mailServer; int mailServerPort; System.Net.Mail.SmtpClient mailClient; System.Net.Mail.MailMessage mailMessage; System.Net.Mail.MailAddress mailFrom; System.Net.Mail.MailAddress mailTo; System.Net.Mail.MailAddressCollection mailToCollection; System.Net.Mail.MailAddressCollection mailCCCollection; System.Net.Mail.AttachmentCollection mailAttachementCollection; System.Net.Mail.Attachment mailAttachment; ; try { toList = strSplit(recipient, ';'); ccList = strSplit(cc, ';'); permissionSet = new Set(Types::Class); permissionSet.add(new InteropPermission(InteropKind::ClrInterop)); permissionSet.add(new FileIOPermission(filename, 'rw')); CodeAccessPermission::assertMultiple(permissionSet); mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName; mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber; mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort); le = toList.getEnumerator(); le.moveNext(); mailFrom = new System.Net.Mail.MailAddress(sender); mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current()))); mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo); mailToCollection = mailMessage.get_To(); while (le.moveNext()) { mailToCollection.Add(strLTrim(strRTrim(le.current()))); } le = ccList.getEnumerator(); mailCCCollection = mailMessage.get_CC(); while (le.moveNext()) { mailCCCollection.Add(strLTrim(strRTrim(le.current()))); } mailMessage.set_Priority(System.Net.Mail.MailPriority::High); mailMessage.set_Subject(subject); mailMessage.set_Body(body); mailAttachementCollection = mailMessage.get_Attachments(); mailAttachment = new System.Net.Mail.Attachment(fileName); mailAttachementCollection.Add(mailAttachment); mailClient.Send(mailMessage); mailMessage.Dispose(); CodeAccessPermission::revertAssert(); info("Email sent."); } catch (Exception::CLRError) { e = ClrInterop::getLastException(); while (e) { info(e.get_Message()); e = e.get_InnerException(); } CodeAccessPermission::revertAssert(); } }
If you’re looking for additional email options, check out Wai Keat Ng’s Dynamics AX Blog. He wrote a useful article which delves deeper into the topic of sending emails through code. You can find his post here:
http://waikeatng.blogspot.com/2010/06/sending-emails-from-dynamics-ax.html
On a separate but related topic, I found a nice post by TJ Vassar about using X++ to email SSRS reports.
How To: Directing Reports to Email:
http://blogs.msdn.com/b/dynamicsaxbi/archive/2012/03/09/how-to-directing-reports-to-email.aspx
LikeLike
Great and helpful post!!
LikeLike
Thanks
LikeLike
Beautiful post, works and is easy to follow.
LikeLike
Great post! Thank you so much.
LikeLike
Great Post!!
For adding multiple email-id can i use container?
and if not then why>?
LikeLike
You don’t need a container to specify multiple email addresses with this code. In the “To” or “CC” fields, just separate multiple email addresses with the semi-colon (;) character.
LikeLike