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:

 

static
public
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   

    try

    {

        WinAPI::moveFile(_PDFLoc, newFileNamex);

    }

   
 

    catch(Exception::CLRError)

        info(strFmt(AifUtil::getClrErrorMessage()));

    //Create Param

   
 

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

    info(param);

   
 

    
 

    
 

    //With file at known loc call GS

   
 

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

   
 

    //now send email with attachment

   
 

    try

    {

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

        itemsClass  = mapifolder.get_Items();

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

        message.set_To(_emailContract.parmTo());

        message.set_Subject(_emailContract.parmSubject());

        message.set_Body(_emailContract.parmBody());

        Message.set_CC(_emailContract.parmCc());

        //Message.set_SentOnBehalfOfName(“dkos”);

        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, “”);

        Message.Save();

    }

    catch(Exception::CLRError)

        info(strFmt(AifUtil::getClrErrorMessage()));  

}

 

 

 

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