How to send an e-mail in AX 7 using x++

I know that is a well known subject when we’re speaking about x++ but it’s always helpful for new developers or when somebody is in a hurry and needs a quick answer.

In the previous versions of AX we had the SysMailer class for send e-mails trough x++ using just two lines of code.

Probably you already know about the several changes that happened for the developer’s point of view in AX7 and this simple task was one of these, but do not worry because it’s still very simple, see the code bellow as an example:

public static void main(Args _args)
{
    SysMailerMessageBuilder mailer  = new SysMailerMessageBuilder();;
    SysMailerSMTP           smtp    = new SysMailerSMTP();
    try
    {
        mailer.setSubject("AX7 - Sending e-mail through x++");
        mailer.setFrom("axforce@axforce.com");
        mailer.setBody("Axforce.wordpress.com");
        mailer.addTo("mchiovitti@axforce.com");
        mailer.addAttachmentFromFile(@"\\10.1.6.249\xml\test.pdf");

        smtp.sendNonInteractive(mailer.getMessage());
    }
    catch(Exception::CLRError)
    {
        error(CLRInterop::getLastException().toString());
    }
}

Silly code but I’m sure that will be helpful for someone.

See you guys next time. 🙂