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

sales order Totals option getting error in Axapta

Problem: On  click sales order Totals option getting below error. Please help me how to solve it. Microsoft.Dynamics.Ax.Xpp.ClrErrorException: Exception of type 'Microsoft.Dynamics.Ax.Xpp.ClrErrorException' was thrown. at Microsoft.Dynamics.Ax.Xpp.CLRInterop.MakeReflectionCall(Object instance, String methodName, Object[] parameters) at Dynamics.Ax.Application.TaxDocumentProxy.Sumbytaxaccountingprovider(TaxAccountingProvider taxAccountingProvider, TaxAcctPostingProfDistributionSide postingSide, String taxType, String taxComponent, Boolean , Boolean , Boolean ) in TaxDocumentProxy.sumByTaxAccountingProvider.xpp:line 15 at Dynamics.Ax.Application.TaxDocumentProxy.@Sumbytaxaccountingprovider(TaxAccountingProvider taxAccountingProvider, TaxAcctPostingProfDistributionSide postingSide, String _taxType, Boolean , Boolean ) at Dynamics.Ax.Application.TaxDocumentProxy.@Sumbytaxaccountingprovider(TaxAccountingProvider taxAccountingProvider, TaxAcctPostingProfDistributionSide postingSide, ...

X++ Code to run SSRS Report with parameter in D365 F&O (Example Sales Invoice )

Below is sample X++ Code to run SSRS Report with parameter in D365 F&O (Example Sales Invoice )      Args                       args = new Args();     CustInvoiceJour            custInvoiceJour;     SalesInvoiceJournalPrint   salesInvoiceJournalPrint;       select firstonly custInvoiceJour where custInvoiceJour.SalesId != '';       // Add record to be printed.     // In order to have the context table we need to set args.record().     args.record(custInvoiceJour);       salesInvoiceController = new SalesInvoiceController();     salesInvoiceController.parmReportName(         PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat());       salesInvoiceContract = salesInvoiceController.parmReportContrac...