21 June 2022

Email the attachments using x++ code in D365FO via SysMailerMessageBuilder, multiple attachments

 class EmailJob

{


    public static void main(Args _args)

    {

        DocuRef     docuRef;

        DocuType    docuType;

        SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();

        Email   toEmail;

        Email   fromEmail;

        SysEmailSystemTable emailSystemTable = SysEmailSystemTable::find("WFEMAIL"); //  system email template email id.

        SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(emailSystemTable.EmailId, emailSystemTable.DefaultLanguage);       

         

        try

        {

            FromEmail = emailSystemTable.SenderAddr;//"abc@3ELogistics.onmicrosoft.com";

            toEmail   = "to@gmail.com";


            messageBuilder.setBody("Hello from D365", false);

            messageBuilder.setSubject("Email Test from D365");

            messageBuilder.addTo(toEmail);

            // Note: not calling setFrom() defaults to the current user for SMTP client, whereas

            // when sent to Outlook any setFrom() value will be ignored since profiles on the client are used

            messageBuilder.setFrom(fromEmail);


            while select docuRef

                where   docuRef.RefCompanyId    == curExt()

                && docuRef.RefTableId           == tableNum(CustInvoiceTable)

                && docuRef.RefRecId      == 5637151326 // CustInvoiceTable.recId

            Exists join docuType

                where docuType.typeId == docuRef.TypeId

                && docuType.TypeGroup == docuTypeGroup::File

            {

                messageBuilder.addAttachment(DocumentManagement::getAttachmentStream(docuRef),docuRef.Name + '.pdf');

            }

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

            // Try using sendNonInteractive method too

        }

        catch (Exception::Error)

        {

            throw error("@SYS33567");

        }

    }


}

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension-...