Friday, 29 March 2019

sending Email alerts in D365 (through SMTP)

class BWP_CreateEmailNotification
{
//main method testing purpose with action menu item
    static void main(Args    _args)
    {
        BWP_CreateEmailNotification     classNotification   =    new  BWP_CreateEmailNotification();
        classNotification.sendEmailNotification();
    }

    public void sendEmailNotification(BWP_BudgetRequest     _budgetRequest = null, Email    _toEmail = "")
    {
        str                     recipient, cc, subject, body;
        subject             =   "Budget adjustment Request";
        recipient           =   _toEmail;
        cc                  =   "";

        body = 'Hi,<br><br>PLease adjust the budget for following Item group.<br><br>';

        body += strFmt('<tr>  PurchId : <td>%1</td>,      ItemGroup : <td>%2</td>,      Requested date : <td>%3</td>,       Comments : <td>%4</td>  <td></tr>',
                    _budgetRequest.PurchId,
                    _budgetRequest.ItemGroupId,
                    _budgetRequest.RequstedDate,
                    _budgetRequest.Comments                 
                    );

        body += '</table><br><br>Thanks.';

        this.send(  recipient,
                    cc,
                    subject,
                    body
                 );
    }

    public void send(str recipient, str cc, str subject, str body, str fileName = '')
    {
        str                                     sender = 'Admin@botswanapost.co.bw';
        List                                    toList;
        List                                    ccList;
        ListEnumerator                          le;

        Set                                     permissionSet;
        System.Exception                        e;

        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, 'rw'));
            CodeAccessPermission::assertMultiple(permissionSet);

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

            le = toList.getEnumerator();
            le.moveNext();

            mailFrom = new System.Net.Mail.MailAddress(sender);
            mailTo  = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current())));
            mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

            mailToCollection = mailMessage.get_To();
            while (le.moveNext())
            {
                mailToCollection.Add(strLTrim(strRTrim(le.current())));
            }

            if(cc != '')
            {
                le = ccList.getEnumerator();
                mailCCCollection = mailMessage.get_CC();
                while (le.moveNext())
                {
                    mailCCCollection.Add(strLTrim(strRTrim(le.current())));
                }
            }

            mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
            mailMessage.set_Subject(subject);
            mailMessage.set_Body(body);
            mailMessage.set_IsBodyHtml(true);

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

            CodeAccessPermission::revertAssert();
        }
        catch (Exception::CLRError)
        {
            e = ClrInterop::getLastException();
            while (e)
            {
                error(e.get_Message());
                e = e.get_InnerException();
            }
            CodeAccessPermission::revertAssert();
        }
    }

}

No comments:

Post a Comment