web
You’re offline. This is a read only version of the page.
close
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

How to send email with attachment x++ code

(0) ShareShare
ReportReport
Posted on by 2

Hi,

I want to send attachment through email. I've tried some piece of code from different but it generate some errors. Unknown error COD.Message & Mailbox unavailable...

If anyone have some verified blog then do suggest. It would be helpful for me.

Regards,

Zohan 

I have the same question (0)
  • Suggested answer
    Sumit Loya Profile Picture
    2,230 on at

    Hi Zohan,

    Did you have a look at class SysEmailDistributor method "processEmails"? This can be a good start.

  • Suggested answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Consider using System.Net.Mail, as demonstrated in How to send email messages with attachments.

  • Verified answer
    Sumit Loya Profile Picture
    2,230 on at

    Here is one sample that works for me.

    static void emailThruSysMailer(Args _args)
    {
        SysMailer   mailer = new SysMailer();
        SysEmailParameters parameters = SysEmailParameters::find();
        System.Exception    clrError;
    
        if (parameters.SMTPRelayServerName)
        {
            mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
                               parameters.SMTPPortNumber,
                               parameters.SMTPUserName,
                               SysEmailParameters::password(),
                               parameters.NTLM);
        }
        else
        {
            mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
                               parameters.SMTPPortNumber,
                               parameters.SMTPUserName,
                               SysEmailParameters::password(),
                               parameters.NTLM);
        }
    
        mailer.fromAddress(parameters.SMTPUserName);
        mailer.tos().appendAddress(toAddress);
        mailer.htmlBody("hi");
        mailer.attachments().add(@'C:\test.jpg');
    
        try
        {
            mailer.sendMail();
            info("Sent");
        }
        catch(Exception::CLRError)
        {
            clrError = CLRInterop::getLastException();
    
            while (clrError != null)
            {
                error(clrError.get_Message());
                clrError = clrError.get_InnerException();
            }
        }
    }

    Or you can also try to use following

    static void emailThruSysMailer(Args _args)
    {
        str fromAddress = "fromAddress";
        str toAddress = "toAddress";
        str subject = "Mail From AX";
        str body = "Howdy Ax User";
        str cc = "ccAddress";
        str attachmentPath = @"C:\test.jpg";
        SysMailer mailer = new SysMailer();
        
        mailer.quickSend(fromaddress, toAddress, subject, body, cc, attachmentPath);
    }

  • Zohan_93 Profile Picture
    199 on at

    Dear Sumit,

    Thankyou. The above code worked for me , anyhow i was missing some sort of relay server name.

    Regards,

    Zohan

  • Zohan_93 Profile Picture
    199 on at

    "You do not have the permission to update this reply" getting this error while trying to verify your answer.

  • nbhatti2001 Profile Picture
    222 on at

    Hi,

    I am trying to use code D365FO, but getting error on the following line. Please help me to fix it.

    SysMailer mailer = new SysMailer();

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    nbhatti2001, you're off-topic here, because this thread is about AX 2012. Please create a new thread in a D365FO forum and explain your problem in detail. But first, look at SysMailerMessageBuilder instead of SysMailer.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans