Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Why emails will not be sent to the destination address from AX through X++ code?

(0) ShareShare
ReportReport
Posted on by 25

Hi to everyone.

Recently, I was assigned to develop the following requirement:

When a payment journal is posted an email is automatically sent to the destination address setup in the E-mail field located in the vendor bank accounts catalog.  

The email itself uses a customized, HTML-content template which data is dynamically filled with a mapping object and displays the detail about the payment done from AX.

So, far I've been able to develop and test successfully this requirement; the email template is filled dynamically with the payment journal's data, and the email is created in the E-mail sending status table.

VendorPayment_5F00_Email_5F00_Waiting.jpg

The problem is that the message remains unsent with the status not changing from Waiting; I've run multiple tests, and the results are the same.

This is a snippet of my code that sends the mail:

SysEmailTable::sendMail(sysEmailTable.EmailId,
currentUserLanguage(),
strLTrim(email),
mapping,
'',
'',
true,
curUserId(),
true);

Then, I went and checked the SysOutgoingEmailTable table and confirmed that the emails were created in the table, however, as you can see from the screenshot below the Message field is left in blank.

pastedimage1649259038900v1.png

Could that be another issue as to why the emails remain waiting in AX and not sending? How can I fill in that field?

I meant another issue due that first I checked the SMTP server configuration, the firewall, and SMTP port to confirm first hand that the problem may or not be originating from there rather than AX.

  • GuidoMTY Profile Picture
    25 on at
    RE: Why emails will not be sent to the destination address from AX through X++ code?

    Thanks to everyone for your replies.

    Recently, I recalled that my company ran into some issues with sending of duplicate emails only in the test servers, henceforth, it was decided to deactivate the batch temporarily while flixing the problem.

    The proposed solution worked, but I needed to switch to another approach with the solution in order to get the same results without making use of the batch functionality.

    Source: Wai Keat Ng's Dynamics AX Blog: Sending emails from Dynamics AX

    The blog in the link above gives a thorough explanation of the different approaches for sending emails from Dynamics AX.

    The approach I formerly implemented for the development's requirement was the SysEmailTable::sendMail(..) method, and this is the AX standard for sending emails through a batch processing functionality, as André already explained in his previous reply.

    So, I chose to go with the System.Net.Mail class for the new approach I implemented into code due to two reasons:

    1. The users only need for the emails to send once they post a payment journal, so that functionality only runs on client and need Not to run on server,
    2. Programming email sending functionality with the System.Net.Mail class is simple and straightforward, as mails can be sent in one get go without any need to run on batch. Additionally, it allows more flexibility to specify multiple recipient address and attaching several files.

    This is the new code sample I went with in order to have email sent straight to the vendor's destination address without batch, and it worked successfully after running several tests.

    client static void sendMail(
    SysEmailId _emailId,
    LanguageId _language,
    str _emailAddr,
    Map _mappings,
    FilenameOpen _attachmentFilename = ''
    )
    {
    SysEmailTable table = SysEmailTable::find(_emailId);
    SysEmailMessageTable message;
    LanguageId languageId;


    str sender = table.SenderAddr;
    str recipient = _emailAddr;
    str subject;
    str body;
    str fileName = _attachmentFilename;

    Set permissionSet;
    System.Exception e;

    str mailServer;
    int mailServerPort;
    System.Net.Mail.SmtpClient mailClient;
    System.Net.Mail.MailMessage mailMessage;
    System.Net.Mail.MailAddress mailFrom;
    System.Net.Mail.MailAddress mailTo;
    System.Net.Mail.AttachmentCollection mailAttachementCollection;
    System.Net.Mail.Attachment mailAttachment;
    ;

    try
    {
    languageId = _language ? _language : table.DefaultLanguage;

    message = SysEmailMessageTable::find(_emailId, languageId);

    if (message)
    {
    subject = message.Subject;
    body = SysEmailMessage::stringExpand(message.Mail, SysEmailTable::htmlEncodeParameters(_mappings));
    body = strReplace(body, '\n', '');

    permissionSet = new Set(Types::Class);
    permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
    permissionSet.add(new FileIOPermission(filename, 'rw'));
    CodeAccessPermission::assertMultiple(permissionSet);

    mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
    mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
    mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);

    mailFrom = new System.Net.Mail.MailAddress(sender);
    mailTo = new System.Net.Mail.MailAddress(recipient);
    mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

    mailMessage.set_Priority(System.Net.Mail.MailPriority::Normal);
    mailMessage.set_Subject(subject);
    mailMessage.set_Body(body);
    mailMessage.set_IsBodyHtml(true);

    if (_attachmentFilename)
    {
    mailAttachementCollection = mailMessage.get_Attachments();
    mailAttachment = new System.Net.Mail.Attachment(fileName);
    mailAttachementCollection.Add(mailAttachment);
    }

    mailClient.Send(mailMessage);
    mailMessage.Dispose();

    CodeAccessPermission::revertAssert();
    }
    }
    catch (Exception::CLRError)
    {
    e = ClrInterop::getLastException();
    while (e)
    {
    info(e.get_Message());
    e = e.get_InnerException();
    }
    CodeAccessPermission::revertAssert();
    }
    }

    pastedimage1649289813576v1.png

    I just want to make clear that all solutions proposed in this thread worked, and all your contributions are welcomed.

    I'm leaving as well other sources on the topic that might help you both with D365 F&O and Dynamics AX 2012.

    Sending email via SMTP using X++ and .NET Framework in Dynamics AX | Corbitech

    Sending Email using X++ code Dynamics Ax 2012 (alirazazaidi.com)

  • GuidoMTY Profile Picture
    25 on at
    RE: Why emails will not be sent to the destination address from AX through X++ code?

    Much appreciated, André.

    In the development server, I overlooked checking the setup for the server configuration and found out that, first, the Is batch server check box was unchecked; I checked it.

    pastedimage1649285319382v2.png

    Second, I went to check the Batch group setup and found that the Batch group Id for the email functionality did not have an AOS instance added yet to run on the server; I added it.

    pastedimage1649285687982v3.png

    After that, I ran more tests, and the emails now sent to the destination address.

    Those were the two missing setup details that prevented the emails from sending.

    Thank you, André.

  • Verified answer
    André Arnaud de Calavon Profile Picture
    292,888 Super User 2025 Season 1 on at
    RE: Why emails will not be sent to the destination address from AX through X++ code?

    Hi Guido,

    The code you used will ensure a table will be filled with emails which needs to be send out. A batch job should be configured and running correctly for the actual processing of the messages in this table. You can read the documentation how to set up the email distribution batch job: Configure email functionality in Microsoft Dynamics AX | Microsoft Docs

    If the batch job is set up already, check if the status of the batch job is in waiting state and if the batch job is linked to a batch group which is linked to an active batch server. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,888 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,772 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans