≡ Menu




Sending Email using X++ code Dynamics Ax 2012

In Dynamics Ax 2012, there is form for configuration parameters. There parameters will further use in sending email. Following is the path of Step.

System Administrator è step upè Email parameters

Email Configuration Dynamics Ax 2012

 

These value stored in Dynamics ax 2012 table SysEmailParameters you can get these values by querying on this table using X++ code.

 

Consider following X++ job which read values form SysEmailParameters and send email using SysMailer api.

static void SendEmail(Args _args)
{
SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject,body;
InteropPermission interopPermission;
SysMailer mailer;
System.Exception e;

;
if (parameters.SMTPRelayServerName)
relayServer = parameters.SMTPRelayServerName;
else
relayServer = parameters.SMTPServerIPAddress;
portNumber = parameters.SMTPPortNumber;
userName = parameters.SMTPUserName;
password = SysEmailParameters::password();
subject = "Subject line for the email";
body = "<B>Body of the email</B>";

CodeAccessPermission::revertAssert();

try
{
interopPermission = new InteropPermission(InteropKind::ComInterop);
interopPermission.assert();
mailer = new SysMailer();
mailer.SMTPRelayServer(relayServer,portNumber,userName,password, parameters.NTLM);
//instantiate email
mailer.fromAddress("ax.notification@mycompany.com");

mailer.tos().appendAddress("alirazazaidi@live.com");
mailer.subject(subject);
mailer.htmlBody(body);
mailer.sendMail();
CodeAccessPermission::revertAssert();
info("Email has been send!");
}
catch (Exception::CLRError)

{
e = ClrInterop::getLastException();

while (e)

{
info(e.get_Message());

e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
//info(e);
info ("Failed to Send Email some Error occure");
}

}

 

{ 0 comments… add one }

Leave a Comment