adsenseheader

Monday, April 10, 2023

Export attachments through Email in D365 Finance and Operations

Hi, If you looking to send an attachmnet like excel, pdf.. etc through code in D365 FO. Below piece can you help you.
public static void sendEmail(str _fileName,
                                 str _subject,
                                 str _body,
                                 System.IO.MemoryStream _memoryStream,
                                 Email _fromEmail,
                                 Email _toEmail)
    {
        SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();
        SysMailerSMTP           smtp = new SysMailerSMTP();
        try
        {
            mailer.setSubject(_subject);

            mailer.setFrom(_fromEmail);

            mailer.setBody(_body);

            mailer.addTo(_toEmail);

            mailer.addAttachment(_memoryStream, _fileName);

            smtp.sendNonInteractive(mailer.getMessage());

            Info("@SYS58551");
        }
        catch (Exception::CLRError)
        {
            System.Exception ex = ClrInterop::getLastException();
            if (ex != null)
            {
                ex = ex.get_InnerException();
                if (ex != null)
                {
                    error(ex.ToString());
                }
            }
        }
        catch (Exception::Error)
        {
            Error("Email sending failed");
        }
    }
str fileNameVal = "ABC_"+ VendTable::find(vendReporting.VendorId).name() + "_" + date2Str(DateStartMth(fromDate),321,2,2,2,2,2) + "_" + date2Str(endMth(toDate),321,2,2,2,2,2) + ".xlsx";

                VendorReportingFileExport::sendEmail( fileNameVal,
                                                                "Inventory and Sales out ",
                                                                "Inventory and Sales out details",
                                                                memoryStream,
                                                                SysEmailParameters::find().SMTPUserName,
                                                                vendReporting.Email);
                

No comments:

Post a Comment