web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Send SSRS report as an email attachment using SysEmailTable

(2) ShareShare
ReportReport
Posted on by 372
Hello all,
I want to send SSRS report using SysEmailTable::send method as an email attachment

        SrsReportRunController controller = new SrsReportRunController();
        MyEmailContract rdpContract = new MyEmailContract();
        SRSPrintDestinationSettings settings;
 
        controller.parmReportName(ssrsReportStr(MyEmailReport, Report));
        controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        controller.parmShowDialog(false);
 
        controller.parmReportContract().parmRdpContract(rdpContract);
       
        settings = controller.parmReportContract().parmPrintSettings();
        settings.printMediumType(SRSPrintMediumType::File);
        settings.fileFormat(SRSReportFileFormat::PDF);
        settings.fileName('Test.pdf');
      
        // Execute the report
        controller.startOperation();
        str path = controller.parmReportContract().parmReportPath();
        SysEmailTable::sendMail("TableBody", "en-us", "Test@Test.com", templateToken, "Test.pdf");


Unfortunately the attachment is not attached and the email is empty :(
Categories:
I have the same question (0)
  • Abhinandhan P S Profile Picture
    17 on at
    Hi,
     
    You can refer the below-mentioned forms.
     
    There was a verified answer. So, can you give it a try?
     
    Thanks,
    Abhinandhan Poorlin Sivakumar.
  • Suggested answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at
    You get a path, but then you ignore it and use "Test.pdf" instead when calling sendMail(). You seem to intend to use the path variable instead.
    If you debug sendMail(), you'll see that the system tries to find a file based on your path, and because no Test.pdf exists, it doesn't add any attachment. You should get used to using the debugger - as you see, just writing some code and checking the final result doesn't tell you what and where failed, but debugging can give you that.
  • Anton Venter Profile Picture
    20,656 Super User 2026 Season 1 on at
     
    Does your code result in a PDF file and does it exist? The sendMail function will try to attach the file if it exists. Have you used the debugger to see what's going on? If your email body is empty, you should use an existing an email template. Is "TableBody" an email template?
     
    SendMail
  • MS-29011540-0 Profile Picture
    372 on at
    @Abhinandhan P S
    Thank you for your reply :)
    I tried it but the PDF was downloaded on my computer (as the system user not on the server itself), but i needed it to be downloaded on the server itself because i would like to use it again to send an email using the file as an attachment. maybe i also missed something. i don't know :)
  • MS-29011540-0 Profile Picture
    372 on at
    @Anton Venter@Martin Dráb
    Thank you for sharing, i actually got benefits when debugging and exploring the sendMail method.
  • Verified answer
    MS-29011540-0 Profile Picture
    372 on at
    I use something like that and i worked with me
    This article helped me:
    https://www.d365finopscodebase.com/2020/01/how-to-dynamically-save-pdf-generated.html

    public static void main(Args _args)
        {
            #define.Note('Note')
            str url;
            SrsReportRunController controller = new SrsReportRunController();
            PayrollContract rdpContract = new PayrollContract();
            SRSPrintDestinationSettings srsPrintDestinationSettings;
            SysCorpNetPrinters netPrinters;
            Filename _filename;
            System.Byte[] reportBytes = new System.Byte[0]();
            SRSProxy srsProxy;
            SRSReportRunService srsReportRunService = new SrsReportRunService();
            SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
            Map reportParametersMap;
            str Note = "Purchase order list report";
            List li = new List(Types::String);
            li.addEnd("ITCO-00001");
            rdpContract.parmRunIDList(li);
            Map templateToken = new Map(Types::String, Types::String);
            templateToken.insert(#Note, Note);
            str tempPath = SysEmailParameters::find().AttachmentsPath;
            tempPath = strFmt("%1\\%2", tempPath, "Pending purchase orders");
            if (!WinAPI::pathExists(tempPath))
            {
                WinAPI::createDirectoryPath(tempPath);
            }
            _filename = strFmt("%1\\Pending purchase orders.pdf", tempPath);
            controller.parmReportName(ssrsReportStr(PayrollReport, Report));
            controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            controller.parmShowDialog(false);
            controller.parmReportContract().parmRdpContract(rdpContract);
            srsPrintDestinationSettings = controller.parmReportContract().parmPrintSettings();
            srsPrintDestinationSettings.printMediumType(SRSPrintMediumType::File);
            srsPrintDestinationSettings.fileFormat(SRSReportFileFormat::PDF);
            srsPrintDestinationSettings.overwriteFile(true);
            srsPrintDestinationSettings.fileName(_filename);
            srsPrintDestinationSettings.pack();
                   
            controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
            controller.parmReportContract().parmReportExecutionInfo(executionInfo);
            srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
            srsReportRunService.preRunReport(controller.parmreportcontract());
            reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
            parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
            srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
            // Actual rendering to byte array
            reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(), parameterValueArray,srsPrintDestinationSettings.fileFormat(SRSReportFileFormat::PDF), srsPrintDestinationSettings.deviceinfo());
            if (reportBytes)
            {
                System.IO.Stream stream = new System.IO.MemoryStream(reportBytes);
                var fileStream = new System.IO.FileStream(_filename, System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);
                stream.CopyTo(fileStream);
                stream.Close();
                fileStream.Close();
                //url = File::SendFileToTempStore(fileStream, _filename, classstr (FileUploadTemporaryStorageStrategy));
                //new Browser().navigate(url, true);
            }
            SysEmailTable::sendMail("PendingPO", "en-us", "Test@Test.com", templateToken, _filename);
        }
     
    Please note that:
    The file must be in this path or a subfolder in the path to got sent by sendMail:
    SysEmailParameters::find().AttachmentsPath

    According to this condition which is inside sendMail:
    if (System.IO.File::Exists(htmlDecodedFileName) &&
                            ((new System.IO.FileInfo(htmlDecodedFileName)).Length < (maxAttachmentSize * 1000000)) &&
                            SysEmailTable::isFromAttachmentsFolder(htmlDecodedFileName))
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans