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('', '', 'your_password', 995, true);
    // Add the account to a list of accounts to be checked
    // 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 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.


Tommy Skaue said...

Nice post, Palle!
If you wanted to encrypt the credentials before they are stored in the database, you could use my example from here:

Palle Agermark said...

Good point Tommy. Thanks.

Now you made me look into what the Brazilian solution does :-)

They have a setup form where you amongst other things set the password, and these settings are stored in a table. When entering a password it is encrypted with the following command:

And when they need the password to the login, it is decrypted with:

These things can be found in \Data Dictionary\Tables\EFDocumentEmailAccount_BR\Methods\editPassword and \Classes\EFDocEmailProcessor_BR\getAccountsToRead.

I have no idea if this is better or worse than your solution ;-)

Tommy Skaue said...

Looks like it is using the same features you can use to protect your web.config application section, meaning it is dependent on the machine it is being encrypted on (unless I am mistaken, this If I am right, you would not be able to decrypt it on a different machine. I wonder if this has been tested for encrypting on server A and decrypted on server B, given a topology with multiple AOSes.

Tommy Skaue said...

If this is correct, the approach in the WinServerAPI is sufficient.

Mine is merely an alternative ^_^

Palle Agermark said...

Hmmm :-)

I guess we'll soon see a hotfix, if that's in fact the case.