Email techniques in AX 4.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

MAPI technique:

Following code demonstrates the usage of mapi class for sending email. It uses outlook to send mail.

static void emailThruMapi(Args _args)
{
    MapiEx      mapiEx;
    MapiExMail  mapiExMail;
    boolean     mapiInitialised;
    COM         outlook;
    COM         item;
    COM         outlookNameSpace;
    COM         folder;
    COM         attachments;
    str         storeId;
    str         entryId;

    #define.outlookapplication(‘Outlook.Application’)
    #define.mapi(‘Mapi’)
    #smmMSOutlook2002ObjectModelConstants
    #define.htmlText(‘<html><body>Hi There</body></html>’)
    ;

    outlook             = new COM (#outlookapplication);
    outlookNameSpace    = outlook.getNameSpace(#mapi);
    outlookNameSpace.logon();

    folder = outlookNameSpace.getDefaultFolder(#olFolderInbox);
    item = outlook.createItem(#olMailItem);
    storeId = folder.storeId();

    mapiEx = new MapiEx();

    if(mapiEx && mapiEx.mapiInitialised())
    {
        mapiInitialised = true;
        if (!mapiEx.logon("","",0) || !mapiEx.openMessageStore(storeId))
        {
            mapiInitialised = false;
            mapiEx.logout();
            mapiEx.finalize();
        }

        //To send mail in HTML format
        item.bodyFormat(#olFormatHTML);
        item.htmlBody(#htmlText);

        //To send mail in plain text format
        //item.body(‘Hi There’);

        item.subject(‘Test mail’);

        //—-Attachements——————-
        attachments = item.attachments();
        attachments.add(‘E:\\Test\\4000.pdf’, 1, 1, ‘4000.pdf’);

        item.saveSentMessageFolder(outlookNameSpace.getDefaultFolder(#olFolderSentMail));
        item.save();
        entryId = item.entryId();

        mapiExMail = mapiEx.getMailFromEntryId(entryId);
        if (!mapiExMail)
        {
            mapiInitialised = false;
            mapiEx.logout();
            mapiEx.finalize();
        }
    }

    if(item)
    {
        if (mapiInitialised && mapiExMail)
        {
            //TO
            mapiExMail.addRecipient(
‘sumit.loya@sumitloya.com’, "", #olTo);
            //CC
            mapiExMail.addRecipient(
‘sreenath.girigari@sreenath.com’,"",#olCC);
            //BCC
            mapiExMail.addRecipient(
‘ashish.singh@ashishsingh.com’,"",#olBCC);

            try
            {
                mapiExMail.save();
                mapiExMail.close();
                mapiExMail.finalize();
                item = outlookNameSpace.getItemFromID(strupr(entryId));

                //This will display the mail item
                //item.display();

                //This will directly send the mail without poping the mail window
                item.send();
            }
            catch
            {
                if (mapiInitialised)
                {
                    mapiEx.logout();
                    mapiEx.finalize();
                }

                // An error occured sending mail from outlook.
                throw error("@SYS97460");
            }
        }
    }
}

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(‘sumit.loya@sumitloya.com’);
    mailer.tos().appendAddress(
‘sumit.loya@sumitloya.com’);
    mailer.body(‘hi’);
    mailer.sendMail();
}

SysInetMail:

SysInetMail internally uses Mapi framework only. But to use SysInetMail one has to setup email templates from Basic –> Setup –> Email templates. SysInetMail will automatically pick sender id, subject, sender name, email text etc. from the email template that you provide while sending from SysInetMail. If you provide a full email address and not the id from Email templates table then also mail will be sent but in that case you need to provide the details yourself.

static void emailThruSysInetMail(Args _args)
{
    SysInetMail mail = new SysInetMail();
    ;

    //To send to an email address directly
    mail.sendMailAttach(
‘sumit.loya@sumitloya.com’, ‘sreenath.girigari@sreenath.com’, ‘Test mail’, ‘Hi There’, false, ‘E:\\Test\\4000.pdf’);
   
    //To use an email template to send mail
    SysInetMail::sendEMail(‘Alerts’, ‘en-us’,
‘sumit.loya@sumitloya.com’);
}

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(‘sumit.loya@solugenix.com’);
    batch.parmEmailAddr(
‘sumit.loya@solugenix.com’);
    batch.parmMessageBody(‘Hi There’);
    batch.parmSubject(‘Test mail’);
    batch.run();
}

SmmOutlookEmail:

This class internally uses Mapi class and is extensively used in HRM module. One feature of this class is that we can specify email groups and it can send mails to all the members defined under this email group. Here is a sample code

static void emailThruSmmOutlookEmail(Args _args)
{
    SmmOutlookEmail smmOutlookEmail = new SmmOutlookEmail();
    ;

    if (smmOutlookEmail.createMailItem())
    {
        smmOutlookEmail.addEMailRecipient(
‘sumit.loya@sumitloya.com’);
        smmOutlookEmail.addSubject(‘Test mail’);
        smmOutlookEmail.isHTML(true);
        smmOutlookEmail.addBodyText(‘<html><body>Hi There</body></html>’);
        smmOutlookEmail.sendEMail(smmSaveCopyOfEmail::No);
    }
}

 

Advertisements

4 thoughts on “Email techniques in AX 4.0

  1. Hi!
     
    I have used the SysMailer like this:
    static void emailQuick()
    {
    SysMailer mailer;
    InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
    ;
    permission.assert();
    mailer = new SysMAiler();
    mailer.quickSend(\’From\’, \’To\’, \’subject\’, \’body\’);
    }
    But if I don\’t use the assert the class doen not work for all users. Why is that, because the permissions are used later in the prosess (in mailser-class)?
     
    What would be your favourite (most robus!) email sending code when sendin a lot of email from AX? Eg. you may not be able to use the Outlook on Mapi types in a Terminal Server setup.
     
    -habio

  2. Hi Habio,
     
    If you do not want to use Outlook then SysMailer is the best way… Actually you need to assert the usage of sys mailer because it internally uses CDO.Message dll so that falls under dangerous APIs and MS has made it mandatory to assert the permissions before its usage for Trust worthy computing. Thats why you need to assert the usage of the SysMailer class and then after its usage revert the assertion back by using CodeAccessPermission::revertAssert()
     

  3. Hi,
    your blog is intereting. I want to ask one thing is that if i use outllo for sending mail so how can i know that whether outlook is installed on the machine or not, if it is installed how i knw it is configured or not. can you please help me if you know how to do this using X++ code to identify the oultook setting on a PC.beause if oultook is not installed or not configured then i wnat to prompt the user with an error.

    Thanks
    Yasir,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s