Monday, January 14, 2013

How to send a report via email from AX 2009


Hi all!

Today I want to show you how to send a custom report via email from AX (it works with an AX client installation and it's tested for Microsoft Outlook and Novell GroupWise).

In AX there is a class that allows you to do this: "SysINetMail".

Here's the code:

void printPDFMail()
{

    SysINetMail     SysINetMail;
    str             FileName;
    str             user;
    str             Body;
    str             Subject;
    str             cc1;
    str             mailAddressFrom;
    str             mailAddressTo;
    UserInfo        userInfo;

    SysINetMail mail = new SysINetMail();
    ;


    select firstonly userInfo where userInfo.id==curUserId(); {
    user= userInfo.networkAlias; }


    FileName = strfmt('C:\\Users\\%1\\Desktop\\AXReportName.pdf',user);

    reportRun.printJobSettings().setTarget(PrintMedium::File);
    reportRun.printJobSettings().format(PrintFormat::PDF);
    reportRun.printJobSettings().fileName(FileName);
    reportRun.run();


    //assign the values ​​of sender, recipient, subject and message body
    Subject     = 'Insert here the email subject';
    Body        = 'Insert here the email body';
    cc1         = 'example@example.com';


    //assign the values ​​to send email
    mailAddressFrom     = SysUserInfo::find(curUserId()).Email;
    mailAddressTo       = email@addresstosend.com

   
    //Send Mail
    mail.sendMailAttach(mailAddressTo,cc1,Subject,Body,false,FileName);

   
    //delete the temporary pdf file
    winAPI::deleteFile(FileName);



    //info("Email sent.");
}

I have included this method in a class that creates the report to be sent, but if you want to use it as a job, you have to use "Args" function to pass the name of the report to be printed.