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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How do I email a batch report?

(0) ShareShare
ReportReport
Posted on by 665

I have a batch report that is scheduled to run nightly on a client's workstation.  Initially I tested this by having the batch job run and the report print every night.  This works fine as long as the client stays logged in with batch processing turned on.  Now I'm trying to take the same report and email it instead of printing it.  I have set the print medium set to E-mail recipient with a valid email address and the message format set to PDF (embed fonts).  When the batch job runs I get 2 Microsoft Outlook security warnings that I have to click allow on but I never get the email.  First, does anyone know how to eliminate the security warnings? Secondly, does anyone have any idea why the email never gets sent?

Gary

*This post is locked for comments

I have the same question (0)
  • Gary Knue Profile Picture
    665 on at
    Re: How do I email a batch report?

    I answered question number 2.  Outlook needs to be running for the email to be sent, if not the email sits in the outbox.  Now I need to figure out how to remove the 2 security warnings.

  • Gary Knue Profile Picture
    665 on at
    Re: Re: How do I email a batch report?

    I just discovered another problem.  Emails sent to an internal email address works fine but when sent to an external email address the pdf attachment is missing from the email.  I see no indication of the attachment being removed by the exchange server.  Any ideas would be greatly appreciated.

  • user5555 Profile Picture
    7,435 on at
    Options: How do I email a batch report?

    Hi,

    1. Alternative: look for 'AutoDoc' alike program. You can send a report to a 'print spooler' which based on some color defined in the report recognizes what to do: send email with report attached as pdf. We use it for digital emailing packingslips and invoices. I by accident came accross other vendor add-on: http://www.dynamics-ax-addons.com/en/add_ons/report_mailer.html

    2. I have come across some code samples that in AX there is an SMTP setting. This means you can directly talk to your Exchange server instead of using an Outlook client. https://community.dynamics.com/forums/t/14809.aspx

    3. Regarding Exchange / Outlook warnings: contact a system admin or somebody specialized in Exchange. I have no idea other than you are not allowed to relay to external addresses.

    J.

  • user5555 Profile Picture
    7,435 on at
    Re: Options: How do I email a batch report?

    Seems that this site is down. Here the cached version for history:"

    [SOURCE: http://dynamic-ax.co.uk/DynamicsAXEmailWithoutOutlook.aspx]

    "

    Sending Emails from Dynamics AX without Outlook

     

    People often ask me if Outlook is necessary to send email from Dynamics AX, and my answer is.. Yes! ‘Out Of the Box’, Dynamics AX does need Outlook installed on the same machine as the AX client to send out emails.

    However Dynamics AX can very easily be customized to send out standard reports via email, without outlook. We are talking about a really small modification here, there is only one method that needs to be edited, hence for clients that find outlook to be too expensive to fit their budget or if there are architectural issues (for example, installing outlook on Terminal Server),this code could be a life saver...  

    Note: AX – Outlook integration is confined to email, there are many other benefits of using outlook with dynamics ax, such as synchronization of crm contacts, transferring appointment to outlook calendar, etc. If the client chooses not to install outlook with Dynamics AX, then the company would be loose out on all these features.

     

    Anyways.. this is what you need to do:

     

    The following method is used to send emails from Dynamics AX:

    Class\Info\Method\ReportSendMail

     

    Out of the box, AX uses the class SysInetMail, which invokes the user email profile to find the outlook / email client that is being used on the system, obviously if it doesn’t find out it would throw an error.

    There are 2 (possibly more, but 2 that I know of) other ways to send out mails from ax (easily).

    1.      

    •      Microsoft.CDO Com Object – Collaboration Data Objects can be used to send email and it  comes pre installed on all AX supported versions of Windows.. as a matter of fact the emails generated by Alerts use the class SysMailer, which in turn uses a CDO object.

    2.      

    •     System.Net.Mail  Assembly – In this example I choose to this path, simply because of the flexibility of the API..  Note:  for this to work System.Net should be a part of the Ax References (AOT > References) ...by default it always is one of AX references, but there is no harm is double checking.

     

    Here is the code... Note:  in order to use this code one needs to agree to these Terms.

     

    //<CHANGE>

    //  <DATE>##/##/###</DATE>

    //  <TRACKING_ID>Mohammed</TRACKING_ID>

    //  <DESCRIPTION>

    //  Email out of ax withot outlook.

    //  the default ax code uses inet to send email and the sysInetMail class check the users profiles for a outlook account.

    //  changing code to use System.Net.Mail assembly

    //  </DESCRIPTION>

    //</CHANGE>

    void reportSendMail(PrintJobSettings p1)

    {

       //Mohammed : Start Declaration

      //SysINetMail m = new SysINetMail();     // Mo : Commented out old AX code

        System.Net.Mail.MailMessage             mailMessage;

        System.Net.Mail.Attachment              attachment;

        System.Net.Mail.AttachmentCollection    attachementCollection;

        System.Net.Mail.SmtpClient              myMail;

        System.Net.Mail.MailAddress             mailFrom;

        System.Net.Mail.MailAddress             mailTo;

        str                                     userMailAddress;

        str                                     receiverMailAddress;

        str                                     mailBody;

        str                                     smtpServer;

        fileNameOpen        fileNameForEmail;

        str                 mail;

        FileIOPermission    perm;

        userinfo            userInfo;

        //end Declaration

     

     

        str fileName = 'axaptareport';

        ;

     

        if (p1.format() == PrintFormat::ASCII)

            fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'TXT'; // Mo : NL

            //fileName = fileName + '.txt'; // Mo Commented this line

        else if (p1.format() == PrintFormat::RTF)

            fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'RTF';

            //fileName = fileName + '.rtf';

        else if (p1.format() == PrintFormat::HTML)

            fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'HTM';

            //fileName = fileName + '.htm';

        //else if (p1.format() == PrintFormat::PDF) // Mohammed :Performance Testing : commentign this line and replacing the line below.

        else if (p1.format() == PrintFormat::PDF || p1.format() == PrintFormat::PDF_EMBED_FONTS)// Mohammed :Performance Testing :(replacing the above line) addign this line as it was present in the jsRemotecontroller project.. can be removedd later..

            fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'PDF';

            //fileName = fileName + '.pdf';

     

        //Mohammed : Start Logic

         mail = subStr(fileNameforEmail,(strlen(fileNameforEmail)-8),9);

         select firstonly name from userInfo where userInfo.id == SysuserInfo::find().Id; // to find the user name

         fileNameforEmail = winApi::getTempPath() + mail; // store attachment in a temp location

         perm = new FileIOPermission(fileNameforEmail,'w');

         if(!perm)

         {

            throw error("Cannot move attachment to temp location.");

            return;

         }

         try

         {

         perm.assert();

         }

         catch

         {

            throw error("Cannot gain access to Temp location.");

            return;

         }

     

         userMailAddress = SysUserInfo::find().Email; // find current users email address setup up in user //options

         receiverMailAddress = p1.mailTo();

         mailFrom = new  System.Net.Mail.MailAddress(userMailAddress,userInfo.name);

         mailTo = new  System.Net.Mail.MailAddress(receiverMailAddress,"");

         mailBody = "Email sent from " + CompanyInfo::name() + ", using Dynamics AX";

         smtpServer = SysEmaiLParameters::find(false).SMTPRelayServerName;// using the SMTP server ip //setup in email Parameters

         mailMessage = new System.Net.Mail.MailMessage(mailFrom,mailTo);

         mailmessage.set_Subject(p1.mailSubject());

         mailmessage.set_Body(mailBody);

     

         //move attachment file to Temp folder

         winapi::moveFile(p1.fileName(), fileNameforEmail);

     

         attachementCollection = mailMessage.get_Attachments();

         attachment = new System.Net.Mail.Attachment(fileNameforEmail);

         attachementCollection.Add(attachment);

     

         myMail = new System.Net.Mail.SmtpClient(smtpServer);

         mymail.Send(mailmessage);

     

         winApi::deleteFile(fileNameforEmail); // delete temp file

     

         CodeAccessPermission::revertAssert();

     

        // Mohammed end

    }

     

  • Gary Knue Profile Picture
    665 on at
    Re: Re: Options: How do I email a batch report?

    Thanks for the replies.  I have tried Microsoft CDO 1.2.1 instead of Outlook but I get the following error:

     Mail error: Unknown MAPI error

    I'm not sure what I'm missing.

  • user5555 Profile Picture
    7,435 on at
    Re: Re: Re: Options: How do I email a batch report?

    Hi,

    I have no experience with it, but strong background in C#.NET development to help assist you.

    I would use the System.Net library instead. That library is 'light' in code and functionality and actually does nothing more than talk to the stmp server based on its message protocol. What the CDO object does looking at MSDN: a lot: getting addresses, etc.

     To lower the complexity and to focus on the only thing that is required: 'send email'. I would use the other dll.

    Please let me know if that helps you. If not. I can wrap the class in a small executable to intercept better the error messages. The messages from the Stmp server are 'string; readable messages'. You can also open telnet (search google or use for instance this (not tried myself, but looks oke: http://www.activexperts.com/activemail/telnet/) and build with simple strings a communication with the smtp server (system.net library does the same, but knows the string syntax). Some system admins know how to do this by heart. Maybe they can help you check the smtp gateway is properly setup and reachable.

     If you know the smtp is reachable, check the values in AX and then in your AX code. Then you can drill down the problem to your dll call system.net.

    Please let me know you need more assistance.

    J.

  • Gary Knue Profile Picture
    665 on at
    Re: Re: Re: Re: Options: How do I email a batch report?

    J,

    Thanks again for your reply.  This is beyond my programming knowledge and I'm not sure there is anyone here to assist me.  Since I can get this to work with Microsoft Outlook I'll concentrate my efforts on that route and try to figure out how to eliminate the 2 security warnings that pop up every time the AX client tries to send an email.

    Gary

  • Gary Knue Profile Picture
    665 on at
    Re: Re: Re: Re: Re: Options: How do I email a batch report?

    I've eliminated the 2 outlook security warnings by installing ClickYes Pro from ContextMagic.com.  The only issue I have left is to figure out how to email the report to a non-outlook user.  Emailing the report internally or externally to an Outlook user works fine but a non-outlook user gets a winmail.dat attachment.  I understand that this is because AX sends the email as RTF, so how do I change AX to send in either HTML or plain text to resolve this issue?

  • user5555 Profile Picture
    7,435 on at
    Redirect: Options: How do I email a batch report?

    Dear Gary,

    Somebody needs help: https://community.dynamics.com/forums/p/29685/50268.aspx. I believe you have build up quite some experience on this matter.

    J.

  • Prasan Kumar Profile Picture
    309 on at
    Re: Redirect: Options: How do I email a batch report?

    hi Mohammed ,

    How to write the same code for server

    Especially this code.

     //move attachment file to Temp folder

        winapi::moveFile(p1.fileName(), fileNameforEmail);

    Please advice me.

    Thanks in adavance

    prasan

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

#2
NNaumenko Profile Picture

NNaumenko 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans