Thursday, July 15, 2021

Email helper class to send email with attachment in Dynamics 365 finance and operations

Its very basic need from customers to send email with attachments for any implementation projects, hope the below helper class will be useful in this case.

/// <summary>

/// email helper class to send email with attachment

/// </summary>

public class EmailHelperClass

{

    public static boolean sendEmail(str _fromEMail, str _toEmail, sysEmailSubject _subject, str _body, str _ccEmail = "", FileName _filename ="", System.IO.Stream _stream = null)

    {

        System.Exception exception;

        try

        {

            var messageBuilder = new SysMailerMessageBuilder();

            messageBuilder.addTo(_toEmail)

                    .setSubject(_subject)

                    .setBody(_body);

       

            if (_ccEmail)

            {

                messageBuilder.addCc(_ccEmail);

            }

            if (_fromEMail)

            {

                messageBuilder.setFrom(_fromEMail);

            }

       

            if (_stream != null)

            {

                messageBuilder.addAttachment(_stream,_filename);

            }

            SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());

        }

        catch (Exception::CLRError)

        {

            exception = CLRInterop::getLastException();

            info(strFmt("@SCM:EmailNotSentSetupInstruction", exception.ToString()));

        }

        return true;

    }

 

}

I will come with another interesting blog post!

No comments:

Post a Comment