20 May 2014

Sending emails with CC and attachment in AX 2012 through X++

// Sending emails with CC and attachment in AX 2012



System administration --> Setup --> System --> E-mail parameters. to set the parameters

static void mailFromAX(Args _args)
{
    str                                   sender    = 'abc@example.com';  // mail id of the sender
    str                                   recipient = '123@example.com';  //mulitple recipients can be specified
    str                                   cc          = 'xyz@example.com';  // mulitple cc id's can be specified
    str                                   subject   = 'Subject of the mail';
    str                                   body      = 'Content of the mail';
    str                                   fileName = @'C:\Attachment.txt';  //Location of the attachment file

    List                                                       toList;
    List                                                       ccList;
    ListEnumerator                                     enumList;  
    Set                                                        permissionSet;
    System.Exception                                  exception;

    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.MailAddressCollection mailToCollection;
    System.Net.Mail.MailAddressCollection mailCCCollection;
    System.Net.Mail.AttachmentCollection   mailAttachementCollection;
    System.Net.Mail.Attachment                  mailAttachment;
    ;

    try
    {
        toList = strSplit(recipient, ';');
        ccList = strSplit(cc, ';');
     
        permissionSet = new Set(Types::Class);
        permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
        permissionSet.add(new FileIOPermission(filename, 'test1'));
        CodeAccessPermission::assertMultiple(permissionSet);

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

        enumList = toList.getEnumerator();
        enumList.moveNext();
     
        mailFrom      = new System.Net.Mail.MailAddress(sender);
        mailTo          = new System.Net.Mail.MailAddress(strLTrim(strRTrim(enumList.current())));
        mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);
     
        mailToCollection = mailMessage.get_To();
        while (enumList.moveNext())
        {
            mailToCollection.Add(strLTrim(strRTrim(enumList.current())));
        }
     
        enumList                 = ccList.getEnumerator();
        mailCCCollection    = mailMessage.get_CC();
        while (enumList.moveNext())
        {
            mailCCCollection.Add(strLTrim(strRTrim(enumList.current())));
        }
     
        mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
        mailMessage.set_Subject(subject);
        mailMessage.set_Body(body);

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

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

        CodeAccessPermission::revertAssert();

        info("Email sent successfully");
    }
    catch (Exception::CLRError)
    {
        exception = ClrInterop::getLastException();
        while (exception)
        {
            info(exception.get_Message());
            exception = exception.get_InnerException();
        }
        CodeAccessPermission::revertAssert();
    }

}

No comments:

Post a Comment