Skip to main content

Sending emails To CC attachment in AX 2012 through X++ code

 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 FromMailAX(Args _args)

{

    str                                  SendFrom    = 'Test@example.com';  // mail id of theSendFrom

    str                                   ToMailIds= 'test222@example.com';  //mulitple recipients can be specified

    str                                   cc          = 'abc@example.com';  // mulitple cc id's can be specified

    str                                   subject   = 'Subject of the mail';

    str                                   body      = 'Content of the mail';

    str                                   fileName = @'C:\Test.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                FromMail;

    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();

       

        FromMail      = new System.Net.Mail.MailAddress(sender);

        mailTo          = new System.Net.Mail.MailAddress(strLTrim(strRTrim(enumList.current())));

        mailMessage = new System.Net.Mail.MailMessage(FromMail, 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();

    }


}

Popular posts from this blog

How to set up parent project in project accounting in ax 2012

create simple project and then go to the project hierarchy fast tab and there you must define sub-project ID format "-#" and then your sub-project will be enabled. For resource assigning in Project first you need to configure the HR module, in that you need to maintain the calendar for those employees and then you need to define the cost of that particular employee (for expense) and for Revenue you need to define the sales price of that particular employee. In the project accounting module you need to mention the calendar in scheduling fast tab and then you need to define WBS, in WBS activity you can be able to assign resources.

ERD Account Payable and Account Receivable in ax 2012