Friday, 10 September 2021

Send email along with attachment in AX 2009 without batch processing

 Today, I will be sharing the code snippet which can utilized to send email along with attachment in a job in AX 2009. 

We will use System.Net.Mail framework and configured email parameters in the AX 2009.

To fill in parameters we will use the form available on this path : Administration > Administration Area > Setup > E-mail parameters.

Code snippet.

static void TestEmai1(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                                     Body;

    str                                     Subject;

    str                                     SMTPServer;

    str                                     FileName;

    FileIOPermission                        perm;

    ;


    mailAddressFrom = new System.Net.Mail.MailAddress("sender email","");

    mailAddressTo = new System.Net.Mail.MailAddress("recipient email","");

    Body = "<B>Body of the email</B>";

    Subject = "Subject line for the email";

    SMTPServer = SysEmailParameters::find(false).SMTPRelayServerName;


    mailMessage = new System.Net.Mail.MailMessage(mailAddressFrom, mailAddressTo);

    mailmessage.set_Subject(Subject);

    mailmessage.set_Body(Body);

    attachementCollection = mailMessage.get_Attachments();


    // Add attachemnts! use double slashes ("\") in the filename path.

    FileName = "D:\\test.xlsx";

    perm = new FileIOPermission(FileName,'w');

    perm.assert();


    attachment = new System.Net.Mail.Attachment(FileName);

    attachementCollection.Add(attachment);

    smtpClient = new System.Net.Mail.SmtpClient(SMTPServer);

    smtpClient.Send(mailmessage);


    CodeAccessPermission::revertAssert();


}

Expected output: 




No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Custom Business events Part 3 - (Activate custom business event) in D365 F&O

 In this blog we will discuss about the steps to activate a custom business in D365 F&O. As we know that business event catalog does not...