Send E-mail using SysMailer class
* Send simple paragrapgh or message using Email Template
static void ProjectAssignmentEmailUsingSysMalier(Args _args)
{
SysEmailParameters parameters;
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
Str1260 subject, body;
SysMailer mailer;
System.Exception e;
SysEmailTable sysEmailTable;
SysEmailMessageTable message;
HcmWorker worker;
ProjTable projTable;
SysEmailId sysEmailId;
Map mappings;
str recepient, messageBody;
// Email template ID
sysEmailId = 'Custom';
parameters = SysEmailParameters::find();
// Return SysEmailTable, e.g Language, sender name, sender email address
sysEmailTable = SysEmailTable::find(sysEmailId);
// Passing RecId here and return the HcmWorker table
worker = HcmWorker::find(5637183005);
// Passing ProjectId here and return the ProjTable table
projTable = ProjTable::find('6805');
if (worker && projTable)
{
// recepient = worker.email();
mappings = new Map(Types::String,Types::String);
mappings.insert('WorkerName', worker.name()); // this will replace variable with actual value.
mappings.insert('ProjectId', projTable.ProjId);
mappings.insert('ProjectName', projTable.Name);
// Return SysEmailMessageTable e.g Email subject, email or template body
message = SysEmailMessageTable::find(sysEmailId, SysEmailTable::find(sysEmailId).DefaultLanguage);
// messageBody is a string type, message.Mail = Email template or body
messageBody = SysEmailMessage::stringExpand(message.Mail , SysEmailTable::htmlEncodeParameters(mappings));
}
if (parameters.SMTPRelayServerName)
relayServer = parameters.SMTPRelayServerName;
else
relayServer = parameters.SMTPServerIPAddress;
portNumber = parameters.SMTPPortNumber;
subject = message.Subject;
try
{
mailer = new SysMailer();
mailer.SMTPRelayServer(relayServer,portNumber, '','', parameters.NTLM);
// Sender email address
mailer.fromAddress(SysEmailTable.SenderAddr);
mailer.tos().appendAddress('emplyee@outlook.com');
// mailer.ccs().appendAddress('emplyee@outlook.com');
mailer.subject(subject);
mailer.htmlBody(messageBody);
mailer.sendMail();
CodeAccessPermission::revertAssert();
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
info ('Email sending is failed');
}
info('Email sent');
}
Comments
Post a Comment