Sunday, 5 January 2014

Sending Email in AX 2012

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;

FileName                        fileName;


;
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>";

    //fileName       = WinAPI::deleteFile("C:\\Users\\jagadis\\Desktop\\Hello.Txt");
CodeAccessPermission::revertAssert();

try
{
WinAPI::createFile("C:\\Expat Import\\Hello1.Txt");

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.fromAddress("jagadis.sahu@mazikglobal.com");
mailer.fromAddress("testaxcrm@expat-group.com");

mailer.tos().appendAddress("jagadis.sahu@gmail.com");
mailer.subject(subject);
mailer.htmlBody(body);
//mailer.attachments().add("C:\\Users\\jagadis\\Desktop\\Hello.Txt");
mailer.attachments().add("C:\\Expat Import\\Hello.Txt");

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");
}

}


No comments:

Post a Comment