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 :)

30 comments:

  1. Hi Dilip, I'm interested in your example .cs code. ;-)
    Can you please mail me that?

    greetz Geert (geert[@]pulse.nl)

    ReplyDelete
  2. Very interresting! I though about this some time but never knew how to read email from code. Can you send me the .cs file? Thanks!

    ReplyDelete
  3. Dilip,
    May I have C# code ?

    Thanks
    Sandip

    ReplyDelete
  4. Hi Dilip,

    I'm also interested in the code. Can you send it to mohamed.tassisto@student.artesis.be

    Thank you,
    M. TASSISTO

    ReplyDelete
  5. Hi Dilip,

    Could you please send me the .cs code, I needed some help on this.


    Thanks & Regards,

    Shafiq Hussain

    shafiqhussain@hotmail.com

    ReplyDelete
  6. Hi Dilip,

    I'm very much interested in the .cs code. Can you email me that one? And I have a question. Is it possible to update tasks / contactpersons via this method?

    Thanks a lot!

    Greetings,

    Marchel (mdleeuw@yahoo.com)

    ReplyDelete
  7. Hi Dilip, can you please send me the .cs code?

    Thanks
    -Shiv

    ReplyDelete
  8. Hi Dilip,

    can you please mail me the C# Code (tob@seelhof.de)?

    Thanks in Advance and Best Regards,
    Tobias

    ReplyDelete
  9. Hello Dilip,

    Even we have a requirement where we need to download a pdf file from outlook email to a shared drive. Can u pls send the code ?

    I never used Microsift dynamics. Do u think I need a microsoft dynamics expert to do this set-up or any dummy can do this code?

    thanks,
    shan (sshanbh@yahoo.com)

    ReplyDelete
  10. Hi Dilip,
    i'm intrested in the example code

    ReplyDelete
  11. Hi Dilip,

    This is great, can i get this .cs file please..

    my email khalid_akkawi@windowslive.com

    ReplyDelete
  12. Can I get the Code please
    gus@kanimambo.com.au

    ReplyDelete
  13. Hello, can I havethe code as well?
    Thanks.

    pepesifuentes@Hotmail.com

    ReplyDelete
  14. Hi Dilip, I'm interested in your example .cs code.
    Can you please mail me that?

    Luiz (ltartarotti@gmail.com)

    ReplyDelete
  15. Hi Can you also please send this code to me ?

    ReplyDelete
  16. Hi Dilip,

    I am having a AX 2012 Customization where i need to read an attachment in the email and load data into AX tables. Please send your code to my mail id : bharatvenkatraman@gmail.com for my reference.

    Thanks in advance
    Bharat V

    ReplyDelete
  17. Hi Dilip, would you send me the .cs and any code you can share with it? Thanks a lot

    Stefan

    stefan.kale[@]polysol.com

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Hi!

    Can you please send me the .cs file?

    Thank you!

    willardalec@yahoo.com

    ReplyDelete
  20. I'd live your example code also. My email is xebbskandalfylla.se

    ReplyDelete
  21. why dont disclose you the code?

    regards,
    panliner shipping

    ReplyDelete
  22. Hi, Dilip!
    I have been looking for such opportunity for several days! Would you be so kind to send me the code?
    My email is farvex@gmail.com

    Thank you in advance.
    Sergey.

    ReplyDelete
  23. Hi, Dilip!

    Very interesting! I have been looking for such opportunity for several weeks!
    Could you please send me the code?
    My email is margus023@gmail.com

    Thank you in advance.
    Margus.

    ReplyDelete
  24. Can I have the code please.
    haroon [dot] attari [at] gmail [dot] com
    Thanks
    Haroon

    ReplyDelete
  25. Hi, Dilip,

    Your post is really appreciated. I need your code. My email address is abdullahagha@hotmail.com

    Regards,
    Abdullah.

    ReplyDelete
  26. Hi, dilip! really appreciate your post kindly send me the code. Email: abdullahagha@hotmail.com

    ReplyDelete
  27. Send me code. email: abdullahagha@hotmail.com

    ReplyDelete