Send Email to Multiple Recipients(To multiple Emails Id ) in Ax 2012 using X++
static void Aks_emailcheck(Args _args)
{
SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject,body;
InteropPermission interopPermission;
SysMailer mailer;
System.Exception e;
Aks_MailTos Aks_MailTos;
List emailAddresses;
ListEnumerator enum;
;
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("akshaykupra@gmail.com");
select Aks_MailTos where Aks_MailTos.RecId == 5537144576;
// i took recid this becouse there must be one line which contain multiple mails id with ","
// like i took it in TosEmails fields
emailAddresses = SysEmailDistributor::splitEmail(Aks_MailTos.TosEmails);
enum = emailAddresses.getEnumerator();
while (enum.moveNext())
{
mailer.tos().appendAddress(enum.current());
}
mailer.ccs().appendAddress("akshaykupra@gmail.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 ("Failed to Send Email some Error occure");
}
}
Regards,
Akshay