Pages

Sunday, May 11, 2014

Read mails from a POP3 account with AX

Often when you browse lists of new AX features and AOT objects, you stumble across something that makes you a bit curious.

In R3 I came across some Brazilian functionality where AX connects to an e-mail account, fetches some official government issued files and processes these.

So AX can read e-mails, eh!?

Oh yes, and the Brazilian stuff is backed by a very nice basic framework for doing so.

Here is a very simple piece of code to show how it works.
static void Job1(Args _args)
{
    SysEmailAccount         sysEmailAccount;
    SysEmailAccountList     sysEmailAccountList = SysEmailAccountList::construct();
    SysEmailReader          sysEmailReader      = SysEmailReader::construct();
    SysEmailMessageReadList sysEmailMessageReadList;
    SysEmailMessageRead     sysEmailMessageRead;
    
    // Setup the account
    sysEmailAccount = SysEmailAccount::newAccount('pop-mail.outlook.com', 'someone@outlook.com', 'your_password', 995, true);
    
    // Add the account to a list of accounts to be checked
    sysEmailAccountList.add(sysEmailAccount);
    
    // Get a list of unread meassages
    sysEmailMessageReadList = sysEmailReader.getUnreadEmailMessages(sysEmailAccountList);
    
    // Loop over the list of unread mesages
    while (sysEmailMessageReadList.moveNext())
    {
        // Get the current message from the message loop
        sysEmailMessageRead = sysEmailMessageReadList.current();
        
        // Work with this message(show the subject line)
        info (sysEmailMessageRead.parmSubject());
    }
}
In a production environment you should of course not keep your account and password in clear text. Look into how the framework works with encrypted values for these, by checking the Brazilian feature.

I tried to test this with my Outlook.com account. If you do that and you have two-step verification turned on, you need to turn off two-step verification while testing or create a new app password to login with. Remember to enable two-step verification again if you disable it.

Also I tried to read messages with the Danish letters "ÆØÅ" - that didn't come out nicely. You probably need to do some extra work with the received text to set the right encoding. I didn't look into this and I'd love to hear if someone does.

In AX under "System administration / Setup / System / E-mail client constraints" you can find some setting of importance to this feature.