Sunday, April 18, 2010

send e-mail through code with attachments in AX

1. To send an email through AX, Initially we need to configure the e-mail parameters in the administration module.

2. If you want to configure an email template. That can also be done from the basic module.

LanguageId LanguageId;
FilePath FilePath;
SysMailer mailer;
SysEmailAddress ToAddress;
SysEmailId EmailId;
new InteropPermission(InteropKind::ComInterop).assert();
FilePath = @'//10.0.23.25/AOSPrintShare/TestSalesOrder.pdf;
mailer = New SysMailer();
mailer.fromAddress(SysEmailTable::find(EmailId).SenderAddr, SysEmailTable::find(EmailId).SenderName);
mailer.subject(SysEmailMessageTable::find(EmailId, LanguageId).Subject);
mailer.tos().appendAddress(Toaddress);
mailer.htmlBody(SysEmailMessageTable::find(EmailId, LanguageId).Mail);
mailer.SMTPRelayServer(SysEmailParameters::find().SMTPRelayServerName,
SysEmailParameters::find().SMTPPortNumber,
SysEmailParameters::find().SMTPUserName,SysEmailParameters::password(),
SysEmailParameters::find().NTLM);
mailer.attachments().add(FilePath);
mailer.sendMail();
CodeAccessPermission::revertAssert();

In the above code, Initially I have given COM object access permission and then i have given the UNC path of the pdf file that has to be attached and assigned it to the file path variable then I have attached the required parameters to the sysmailer class and then performed sending e-mail functionality and then reverted back the code access.

2 comments:

Muhammad Yasir said...

Hi,

your blogs are really interesting.
One thing i would like to know that how can i check in AX whther outlook is installed on a machine or not, if yes then wether it is configured for sending mails or not. please let me know any code regarding that.

Thanks,
Yasir

Muhammad Yasir said...

Hi,

your blogs are really interesting.
One thing i would like to know that how can i check in AX whther outlook is installed on a machine or not, if yes then wether it is configured for sending mails or not. please let me know any code regarding that.