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

Announcements

No record found.

News and Announcements icon
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
    239,067 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
    239,067 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 495 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 315 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans