Blog

Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Sunday, February 16, 2020

Send Email through code using System.Net.Mail in ax 2012

public void sendEmailNew()
{
    str                                   sender    = 'it@****.com';  // mail id of the sender
  martina.merola@obagservice.com';  //mulitple recipients can be specified
    str                                   recipient = '***@****.com; ****@****.com; ****@****.com';
    str                                   cc          = '****@****.com; ****@****.com';  // mulitple cc id's can be specified
    str                                   subject   = '**** Sales Data';
    str                                   body      = 'Hello **** Team,\n\r Kindly find the attached file as **** Sales Data with this mail. \n\r NOTE: - This is a system generated email. \n\r Regards, \n\r **** IT Team';
    str                                   fileName; // = @'C:\email attachment\****SalesData 20200211.csv';  //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;
    ;

    fileName    = @'C:\email attachment\****SalesData '+date2str(fromDate,321,
                                                    DateDay::Digits2,
                                                    DateSeparator::None,
                                                    DateMonth::Digits2,
                                                    DateSeparator::None,
                                                    DateYear::Digits4)
                                                    //+"to"+date2str(toDate,321,
                                                    //DateDay::Digits2,
                                                    //DateSeparator::None,
                                                    //DateMonth::Digits2,
                                                    //DateSeparator::None,
                                                    //DateYear::Digits4)
                                                    +'.csv';
 
    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();
    }
}

1 comment:

  1. Very Well, This is the best i have ever seen, you explain very well. Totally Impressive. Israel VPS Hosting

    ReplyDelete