The Infolog

A blog of Dynamics AX development tips and tricks

Skip to: Content | Sidebar | Footer

Using the Outlook API to produce drafts

29 September, 2013 (15:22) | Uncategorized | By: Howard Webb

I have been playing a bit with the email process of an SSRS report in AX 2012 as a self-learning exercise. Thought it might be worthwhile to share the code I have written so far. Using real life examples of requested functionality I have changed the SalesInvoice report to:


  • Create an Email and save it to the drafts folder of Outlook
  • Create the invoice as a secured PDF
  • Add an additional attachment to the email


To do this I made a change to \Classes\SrsReportRunPrinter\toEmail\:



And then here is my Method:


void EncryptPDF(  FilePath                    _PDFLoc,

                                SrsReportEMailDataContract  _emailContract)





    Microsoft.Office.Interop.Outlook._Application                                           outlookApp;

    Microsoft.Office.Interop.Outlook.ItemsClass                                             itemsClass;

    Microsoft.Office.Interop.Outlook.Attachment                                             attachment;

    Microsoft.Office.Interop.Outlook.Attachments                                            attachments;

    Microsoft.Office.Interop.Outlook.MailItemClass                                          mailItemClass;

    Microsoft.Office.Interop.Outlook.NameSpaceClass                                         Nspace;

    Microsoft.Office.Interop.Outlook.MAPIFolder                                             mapiFolder;

    int                                                                                     numEmails;

    int                                                                                     i;

    System.String                                                                           dotNetString;

    str                                                                                     xPlusPlusString;

    Microsoft.Office.Interop.Outlook.MailItem                                               message;

    Microsoft.Office.Interop.Outlook.AddressEntry                                           sender;

    Microsoft.Office.Interop.Outlook.Items                                                  item;


    FilePath    GSDir;

    Filename    GS;

    Filename    PDFName;

    FilePath    TMPDir;

    FilePath    outputDir;

    str         param;

    str         param1;

    str         param2;


    Filename    xyz = System.IO.Path::GetFileName(_PDFLoc);

    str         newFileNameX;




    new InteropPermission(InteropKind::ClrInterop).assert();

    outlookApp  = new Microsoft.Office.Interop.Outlook.ApplicationClass();

    Nspace      = outlookApp.GetNamespace(‘Mapi’);

    //Set values     

    outputdir   = “C:\\temp\\Secure\\”;

    TMPDir      = “C:\\temp\\UnSecure\\”;


    GS          = “gswin64.exe”;

    GSDir       = “C:\\Program Files\\gs\\gs9.02\\bin\\”;

    param1      = “-dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOwnerPassword=password1 -dKeyLength=128 -dEncryptionR=3 -dPermissions=-3904 -sOutputFile=C:\\temp\\Secure\\”;


    newFileNamex = TMPDir + xyz;   


    //first pickup PDF and move it to TMP   



        WinAPI::moveFile(_PDFLoc, newFileNamex);





    //Create Param


    param = param1 + xyz + ” “ + TMPDir + xyz;





    //With file at known loc call GS


    WinAPI::shellExecute((GSDir + GS), param);


    //now send email with attachment




        mapiFolder  = Nspace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders::olFolderDrafts);

        itemsClass  = mapifolder.get_Items();

        message     = outlookapp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType::olMailItem);






        attachments = message.get_Attachments();

        attachments.Add(@outputdir + xyz, Microsoft.Office.Interop.Outlook.OlAttachmentType::olByValue, 1, “”);

        attachments.Add(@”C:\\temp\\HWEB.pdf”, Microsoft.Office.Interop.Outlook.OlAttachmentType::olByValue, 2, “”);









It’s a pretty rough and ready example and this is no more than a quick POC but you get the idea.

Print Friendly, PDF & Email