Sending mail from AX using .NET Framework

Sending mail from AX using .NET Framework

Sometimes happen that SysMailer class (using CDO) is not the right solution for sending mails with attachments. There is a little sample of X++ Job that is using System.Net.Mail namespace to achieve same.

static void JobNETSendMail(Args _args)
    System.Net.Mail.MailMessage             mailMessage;
    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;
    System.Net.Mail.SmtpClient              smtpClient;
    System.Net.Mail.MailAddress             mailAddressFrom;
    System.Net.Mail.MailAddress             mailAddressTo;
    str                                     strBody;
    str                                     strSMTPServer;
    str                                     strFileName;
    FileIOPermission                        perm;

    // preparing parameters
    mailAddressFrom = new System.Net.Mail.MailAddres"", "");
    mailAddressTo = new  System.Net.Mail.MailAddress("","");
    strBody = "There is a email body";
    strSMTPServer = "MailServerName";
    // preparing mail with body, subject, from, to.
    mailMessage = new System.Net.Mail.MailMessage(mailAddressFrom, mailAddressTo);
    mailmessage.set_Subject("There is a email subject");
    attachementCollection = mailMessage.get_Attachments();

    strFileName = "C:\\path\\filename";
    // assert permision
    perm = new FileIOPermission(strFileName,'w');

    // attaching file to that email.
    attachment = new System.Net.Mail.Attachment(strFileName);
    smtpClient = new System.Net.Mail.SmtpClient(strSMTPServer);
    // release permision



Popular posts from this blog

Connecting to Databases through X++ PART -I

X++ code for document attachment

Multiple Tables In Query