Emailing in X++

Below is a static class method created to send emails in AX:


static void SendMessage(str _email, str _msg, str _sub)
{
    SysMailer                               mailer;
    InteropPermission                       iperm;
    str                                     error;
    ;

    try
    {
        iperm = new InteropPermission(InteropKind::ComInterop);
        iperm.assert();

        mailer = new SysMailer();
        mailer.SMTPRelayServer("smtp.youremailserver.com");
        mailer.fromAddress("sendfromaddress@yourdomain.com");
        mailer.tos().appendAddress(_email);
        mailer.htmlBody(_msg);
        mailer.subject(_sub);
        mailer.sendMail();

        CodeAccessPermission::revertAssert();
    }
    catch
    {
        CodeAccessPermission::revertAssert();

        error = "Unable to connect to SMTP server. " + date2str(today(), 321, 2, DateSeparator::Slash, 2, DateSeparator::Slash, 4) + " | " + time2str(timenow(), TimeSeparator::Colon, TimeSeparator::Colon);
    }
}