web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

X++ code to send simple email from D365 FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post we will view to send simple email using X code from D365FO. This code can be used to test the email functionality. 

Note: This post doesn't contain the settings & configuration for email setup which will be explained in another post.

X code:

class DAX_SendSimpleEmail
{        
    /// 
    /// Runs the class with the specified arguments.
    /// 
    /// The specified arguments.
    public static void main(Args _args)
    {
        SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();
        Email					fromEmail = "sender@domain.com";
        Email					toEmail = "recipient@domain.com";
        str						subject = "Sample email";
        str						body = "Email body";

        try
        {
			// Sender email
            messageBuilder.setFrom(fromEmail);
			// Subject
            messageBuilder.setSubject(subject);
			// Body
            messageBuilder.setBody(body, false);
            // Recipient email
            messageBuilder.addTo(toEmail);
            
            SysMailerFactory::sendInteractive(messageBuilder.getMessage());
        }

        catch (Exception::Error)
        {
            throw error("@ApplicationFoundation:EmailProviderNotFound");
        }
    }

}

Output:

On executing the above code, we receive the below output. We have to choose the first option, click on Ok button to proceed further.

EmailOutput.jpg

Regards,

Chaitanya Golla

Comments

*This post is locked for comments