Total Pageviews

Search This Blog

Wednesday, December 15, 2010

[Updated] Code to read email using EWS (Exchange Web Services)

Thanks for all my blog readers as I got an overwhelming response for this post asking me for the code to connect using EWS and read email from Exchange Server

For starters, this is the Part 1 which gives a theoretical view of using EWS to read email without having Outlook Client on your AOS
http://daxdilip.blogspot.com/2010/12/how-to-read-email-and-download.html

This is all the code which does the job:

a. Declaration







b. Logic for reading the email for a particular email address and subject and downloading the attachment















As you have seen the code above is very self-explanatory as it calls the Exchange Service proxy to do a FindItems for the mailbox folder (Inbox) and for a particular subject.

The next part in code is where it loops through the item collection and if it finds an attachment for the message item, it will download the attachment to the path supplied.

Limitations:

a. I tried to directly invoke the code from AX by adding the Exchange Service Assembly to the Resources node in AOT, but I'm unable to marshall the complex types and generics between X++ and .Net, Also the class ContainsSubstring is an abstract implementation in .Net which I'm unable to get a handle via X++, so I had to write a custom .Net wrapper and then call this from X++.

b. The X++ code which invokes my custom .Net assembly works fine without any issues if it's run via a job or class or menu item but as soon as I put it in a batch it doesn't work. It just fails without any error log.

[Update] I was able to track the error (CLR Exception: "When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids") which was caused due to impersonation as the batch job was running under the AOS Service account and not the mailbox account, once I wrote the impersonation code using System.Net.Credentials in my .Net code, it works now without any issues.

Saturday, December 11, 2010

How to Read email and download attachment from AX (AOS) without Installing Outlook Client and using EWS Exchange Web Services

Design Requirement

To meet one of our solution requirement, we had to built an interface in AX which should be able to read an email with a particular subject line and download the invoice attachment to do further processing and posting of the same.


I searched on the web and most of the existing solutions or blog posts are about AX Sending emails where AX Communicates to Microsoft Exchange Server and is tightly coupled with Outlook client to be installed on AOS or client.

In our server topology, our infrastructure/IT team at the client's place won't allow us to install any office tools on the server, thereby ruling out the option of installing Outlook Client on the server.

Solution

After some research, I found that EWS is the way to go. EWS stands for Exchange Web Services which is an extensible component for clients to connect to your Exchange Server and can read emails from a particular mailbox. Here are the steps:



a. Download EWS SDK by going to this url http://www.microsoft.com/downloads/en/details.aspx?FamilyId=190F71A4-7B5F-4A4C-99BA-9BD032E16E15&displaylang=en,
Note, we have a separate SDK for EWS 2010. I downloaded the above SDK as our client is still on Exchange Server 2007

b. Install the Exchange Service msi which copies the Exchange Web Service dll to C:\Program Files\Microsoft\Exchange folder

c. Retrieve this dll and copy to your assembly on the AOS Server. i.e. you can drag and drop this dll file into C:\Windows\assembly

d. Note that this assembly is a .Net assembly so for me the easiest way was to write a custom wrapper assembly in C# which consumes the Microsoft Exchange Web Service (i.e. the dll in step b)

e. Add this custom dll into your References node in AOT

f. Write a class in AX which invokes your custom assembly


g. In my scenario, all the code had to achieve is given the parameters of email id, subject and UNC path name (Network location), it looks up the Exchange Store, reads the email with that particular subject line and downloads the related invoice attachment and strips it to the folder location on the network



And all this achieved without any mess and just few lines of code. If anybody interested in code, feel free to write to me and I can send you the .cs file



Happy Dax'ing :)

Saturday, December 4, 2010

Dynamics AX 2009 New VM - November release

Couple of days back I tried to open my existing Dynamics AX VM to find that it expired on Nov 15.

Lachlan has put up a blog post about the new VM's which Microsoft has released here in this link http://blogs.msdn.com/b/lcash/archive/2010/11/03/announcement-of-microsoft-dynamics-ax-2009-demonstration-vm-november-2010-refresh.aspx

VM OS now comes in 3 flavors

a. Hyper-V with Windows Server 2008
b. VPC 2007 with Windows Server 2008
c. VPC 2007 with Windows Server 2003

You can jump to PartnerSource by clicking these links

a. https://mbs.microsoft.com/partnersource/deployment/documentation/howtoarticles/MSDYAX2009_DemoToolkit_Refresh4

b.
https://mbs.microsoft.com/customersource/documentation/setupguides/MSDYAX2009_DemoToolKit_Refresh3_5

c.
https://mbs.microsoft.com/customersource/documentation/setupguides/MSDYAX2009_DemoToolKit_Refresh3

Fiddler - Good tool to check EP web page, custom websites, performance and AIF Web services

Recently, I was evaluating some tools for troubleshooting performance related to EP web pages.

I just bumped upon this site to find some great tools for testing performance of webpages, websites, sharepoint sites etc.

http://blogs.msdn.com/b/sanjaynarang/archive/2009/04/18/sharepoint-performance-stress-load-testing.aspx


Out of the links in the browser add-ons, I evaluated Fiddler   http://www.fiddler2.com/Fiddler2/firstrun.asp and it's quite handy, after installation it sits quietly in the browser window and then each page you load, it will show you the statistics in terms of how much time each functionality took to load, like web controls, web menus etc.

Good handy tool for any website debugging, Also with AIF Web Services, it shows you the xml sent between calls.

Friday, December 3, 2010

Troubleshooting - AOS Restart

Issue:
In Dynamics AX 2009, I modified an existing table to add 2 new fields, as usual I did a db synch on that table so that the table definition is updated.

Came back to my program  which is a class which populates this table. Somehow, the values were not getting populated in these 2 new fields. Whatever, I tried but the program (my class) simply doesn't update my two new fields. Even, I went to the extent of hard coding these field values, but still it doesn't put any values in the table for those two new fields. Went into debugger mode and watched the tables populating the fields properly but after the insert statement still the values for those 2 new fields are not updated.

Solution:
After struggling for 30 mins, finally gave up and did an AOS restart and there you go! the values now get updated