Interview Questions

Monday, May 12, 2014

Email techniques in AX 5.0

In this article, I am going to demonstrate different email techniques that can be used in AX 4.0. Following classes can be used to send an email
  • Mapi and its related classes
  • SysMailer
  • SysInetMail
  • SysEmailBatch
  • SmmOutlookEmail
SysEmailBatch:
SysEmailBatch internally uses SysMailer class and is used to send emails in batch. That is this class is batchable. Here is a small example for the class
static void emailThruSysEmailBatch(Args _args)
{
    SysEmailBatch   batch = new SysEmailBatch();
    ;

    batch.parmSenderAddr("aslam.p@XYS.net");
    batch.parmEmailAddr("aslam.p@p@XYS.net");
    batch.parmMessageBody("Hi There");
    batch.parmSubject("Test mail");
    batch.run();
    info('Done');
}


SysMailer:
In the following code you can see how to use SysMailer class for sending mails. To use SysMailer class you need to set Relay server or computer name, user name and password in Administration –> Setup –> Email parameters form. This class internally uses CDO.Message dll for communication purposes. Please note in AX 3.0 SysMailer uses Dundas.Mailer dll for communication.
static void emailThruSysMailer(Args _args)
{
    SysMailer   mailer = new SysMailer();
    SysEmailParameters parameters = SysEmailParameters::find();
    ;

    if (parameters.SMTPRelayServerName)
    {
        mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
                           parameters.SMTPPortNumber,
                           parameters.SMTPUserName,
                           SysEmailParameters::password(),
                           parameters.NTLM);
    }
    else
    {
        mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
                           parameters.SMTPPortNumber,
                           parameters.SMTPUserName,
                           SysEmailParameters::password(),
                           parameters.NTLM);
    }

    mailer.fromAddress("aslam.p@XYZ.net");
    mailer.tos().appendAddress("aslam.p@XYZ.net");
   // mailer.body("hi");
    mailer.sendMail();
    info('Done');
}
static void Email(Args _args)
{
    str ToAddress =  "aslam.p@infotech.net";
    str FromAddress = "aslam.p@infotech.net";
    str Subject = "Journal not posted";
    str Body ;
    str FileToAttach = 'C:\\Users\\aslam.p\\Desktop\\Report AX 2012, data processing_files\\A.Txt';//
    SysMailer  mailer = new SysMailer();
    ;
    Body = "<B>Body of the email</B>";
    mailer.quickSend(fromaddress,toaddress,subject,body,toaddress,FileToAttach);
    info("Done");
}
static void EmailsendwithAttachment(Args _args)
{
    Args args;
    ReportRun rr;
    str reportName = "Report2";
    str myPath,pdfFileName;
    str ToAddress =  "aslam.p@infotech.net";
    str FromAddress = "aslam.p@infotech.net";
    str Subject = "Journal not posted";
    str Body,messageBody ;
    SysMailer  mailer = new SysMailer();
    ;
    messageBody = "<p>Dear All,</p><P> Please find the errors in ledger journal report attached.</p>";
    messageBody += "<p><font size=3 face=Garamond>Regards,</font></p><p><strong><font size=3 face=Garamond>AX Admin</font></strong></p>";
    messageBody += "<p><strong><font size=3 face=Garamond>Microsoft Dynamics AX</font></strong></p>";
    messageBody += "<p><strong><font color=#808080 size=3 face=Tahoma>Hospital</font></strong></p>";
    body = strfmt("%1",messageBody);
    startLengthyOperation();
        args = new Args(reportName);
        args.caller(rr);
        rr = new reportRun(args);
        rr.query().interactive(false);
        rr.report().interactive(false);
        rr.setTarget(printMedium::File);
        rr.printJobSettings().setTarget(PrintMedium::File);
        rr.printJobSettings().preferredTarget(PrintMedium::File);
        rr.printJobSettings().format(PrintFormat::PDF);
        rr.printJobSettings().warnIfFileExists(false);
        rr.printJobSettings().suppressScalingMessage(true);
        pdfFileName = @"C:\\Users\\aslam.p\\Desktop\\Report1.pdf";
        rr.printJobSettings().fileName(pdfFileName);
        rr.init();
        rr.run();
    endLengthyOperation();
    info("Report hasbeen saved");
    mailer.quickSend(fromaddress,toaddress,subject,body,toaddress,pdfFileName);
    info("Done");

}
static void email(Args _args)
{
    // For email notification
    str                         messageBody;
    NWH_XmlImpErrorFlag         XmlImpErrorFlag;
    InteropPermission           permission = new InteropPermission(InteropKind::ComInterop);
    SysMailer                   mailer;
    str                         Body;
    str                         ToAddress;
    str                         FromAddress = "aslam.pasha@infotech.net";
    str                         Subject = "Integration Alerts";
    int                         x;
    H_IntegrationParameters   parameters;
    Container                   id;
    LedgerJournalTrans          ljt;
    date                        checkdate;
    Real                        CheckDr , CheckCr ;
    int i,j;
    ;
    try
    {
       info("In the 'try' block. (j1)");
       info("infolog!");
       throw warning("test warning");
       throw error("test error");
       info("infolog!");
       throw warning("test warning");
        //throw Exception::Error;
    }
    catch
    {

        for (i=1; i<=infolog.line(); i++)
        {
            messageBody = ":" + infolog.text(i);

        }
        parameters = H_IntegrationParameters::find();
        id = str2con(parameters.HSEmailId,",");
        for(x=1 ; x <= conlen(id); x++  )
        {
            toaddress   =  strRem(conpeek(id,x)," ");
            Body = strfmt("%1", messageBody) ;
            CodeAccessPermission::revertAssert();
            permission.assert();
            mailer = new SysMailer();
           // CodeAccessPermission::revertAssert();
            mailer.quickSend(fromaddress,toaddress,subject,Body);
        }

    }
}

No comments:

Post a Comment