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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Customize print destination settings form (SRSPrintDestinationSettingsForm) to send emails with sales confirmation Report along with static PDF in D365FO

(0) ShareShare
ReportReport
Posted on by 20

Hello everyone,

I have a requirement that i have to add few fields in print destination settings form for sending attachments(sales confirmation report PDF along with a static pdf). I have added the fields but i stuck at how to send email with these attachments. I found a method SrsReportRunPrinter\toMail() but it is a private method. Cannot write CoC or pre post event handlers and it is not hookable. Do anyone know how to handle this ? Thanks.

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi NithinPeddi,

    "ToEmail" method will not help too much you, because sending it done in SrsReportRunMailer.EmailReportWithRetry and it can send only one file.

    Probably the easiest solution will be to print file to archive and then make own sending method to send all attachments.

    Or you can try to add new value to enum SRSPrintMediumType (e. g. EmailWithAttachements), subscribe to delegate in onPrintReport method and copy, adjust and call "ToEmail" method to your needs.

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    moved to the d365 forum

  • Suggested answer
    Mea_ Profile Picture
    60,284 on at

    One of the possible solutions is to write you own mailer (derived from SrsReportRunMailer) overwrite emailReport method to do whatever you need and then wrap SrsReportRunPrinter.parmReportRunMailer method using CoC to return your mailer instead of standard.

  • NithinPeddi Profile Picture
    20 on at

    Hello Ievgen,

    Thanks for your response. I need to attach another static PDF (stored on shared location) along with the confirmation report. I am able to attach confirmation report but i doesn't know how to attach the other PDF. Please let me know. Thanks,

  • WillWU Profile Picture
    22,361 on at

    Hi NithinPeddi,

    Have a look at the method emailReport() from the SrsReportRunMailer class, as you can see in the following piece of code, this is the out-of-the-box method to add attachments and send emails:

    public boolean emailReport(SrsReportEMailDataContract emailContract, System.Byte[] reportBytes, str fileName)
    {
        SRSReportFileFormat fileFormat;
        boolean result = false;
    
        // Check args and validate contract
        if(!emailContract)
        {
            throw error(strfmt("@SYS318601", 'emailContract'));
        }
    
        if(reportBytes.Length == 0)
        {
            throw error(strfmt("@SYS318601", 'reportBytes'));
        }
    
        if(!fileName)
        {
            throw error(strfmt("@SYS318601", 'fileName'));
        }
    
        emailContract.validate();
    
        // Using mailer to send out report non interactively.
        if(mailer == null)
        {
            this.initMailer();
        }
    
        if (mailer)
        {
            // Construct mailer message builder and pass all the values from the contract.
            var messageBuilder = new SysMailerMessageBuilder();
            messageBuilder.setFrom(fromAddress)
                          .addTo(emailContract.parmTo())
                          .addCc(emailContract.parmCc())
                          .setSubject(emailContract.parmSubject())
                          .setBody(emailContract.parmBody())
                          .addAttachment(new System.IO.MemoryStream(reportBytes), fileName);
            result = mailer.sendNonInteractive(messageBuilder.getMessage());
        }
    
        return result;
    }

    You could extend the SrsReportRunMailer class and get your file as a Memory stream and send the stream as a attachment.

    System.Byte[] binData1;
    System.IO.Stream stream1;
    // Turn the Bytes into a stream
    for(int i = 0; i < conLen(binData); i  )
    {
    binData1 = conPeek(binData,i 1);
    stream1 = new System.IO.MemoryStream(binData1);
    }
    var messageBuilder = new SysMailerMessageBuilder();
    messageBuilder.addAttachment(stream1,'abcd.pdf');

    Refer to the following blog:

    https://allaboutmsdynamics.wordpress.com/2018/08/10/d365-ax7send-email-through-x-code/

    Hope this helps.

  • NithinPeddi Profile Picture
    20 on at

    Hello Will Wu,

    Thank you so much for your reply. I am able to attach the REPORT but not external PDF which is stored in a shared location (Terms and Conditions). Could you please help me on how to convert an external PDF into Bytes ? Thanks.

  • WillWU Profile Picture
    22,361 on at

    Hi NithinPeddi,

    Please try the following code:

    using Microsoft.Azure;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    using Microsoft.WindowsAzure.Storage.File;
     
    class RunnableClass3
    {
     
        public System.IO.Stream downloadfilefromAzure(str _filename)
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            //Setting connection
            var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials('AzureStorageAccountName', 'AzureStorageAccountKey');
            CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            //main folder name
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare share = fileClient.GetShareReference('AzureStorageShareName');
            if (share.Exists(null, null))
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                CloudFileDirectory fileDir = rootDir.GetDirectoryReference('folder');
     
                if (fileDir.Exists(null, null))
                {
                    CloudFile file = fileDir.GetFileReference(_filename);
     
                    if (file.Exists(null, null))
                    {
                        file.DownloadToStream(stream, null, null, null);
                    }
                }
            }
            return stream;
        }
    
    }

    Refer to the DOC:

    https://docs.microsoft.com/zh-cn/azure/storage/files/storage-dotnet-how-to-use-files

    Hope this helps.

  • NithinPeddi Profile Picture
    20 on at

    Hi Will Wu,

    I been through the docs and your sample code. We are not using azure blob storage (it's a On-Prem implementation not cloud). The PDF that have to be converted into bytes will be stored on our own servers. We will store this static PDF in a shared location and i need to pick this document at run time to send it as an attachment to the customer. Please let me know if there is any other workaround. Thanks.

  • WillWU Profile Picture
    22,361 on at

    Hi NithinPeddi,

    Have a look at this code to convert a file to byte.

    str path = @"C:\MyFile.pdf";
    
    byte[] a = File.ReadAllBytes(path);

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans