C#: Getting All Emails From Exchange using Exchange Web Services

Email_OpsThis article demonstrates how to use Exchange Webservices (EWS), as found on Exchange 2007 SP1 and going forward, to extract email body, headers and other email related items without using or needed Outlook installed. Note this assumes you are using Visual Studio 2008 and C#.

Steps

  1. Download and install the EWS assemblies from Exchange Web Services Managed API and install the target version either 32 or 64. Please remember to note the directory which it gets installed to for it is needed later. Of note is that the install directory contains an excellent document Getting Started which has examples that show the capabilities of the web services.
  2. Create your project in Studio, the below example C# code targets a Console Application.
  3. Add a reference to Microsoft.Exchange.WebServices by browsing to the directory loaded in step 1 and selecting the  Microsoft.Exchange.WebServices.dll. Remember to add using Microsoft.Exchange.WebServices.Data; in your code.
  4. Add these lines of code to initialize the web service:
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    
    //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
    
    service.AutodiscoverUrl( "First.Last@MyCompany.com" );

    • Line 1 : Create an Exchange Web Service instance specifying that we are targeting a specific version of Exchange.
    • Line 3 :   You may need to have a service account do your dirty work and this line uncommented out is your key to doing that.
    • Line 5 : We don’t specify an Exchange server, but auto discover it. The server may be clustered and this will get the best one for the job. We also specify the account’s mailbox to use.
  5. Once the service is up and running we can now get the emails found in the box. Here is how we do it:

    FindItemsResults<Item> findResults = service.FindItems(
       WellKnownFolderName.Inbox,
       new ItemView( 10 ) );
    
    foreach ( Item item in findResults.Items )
       Console.WriteLine( item.Subject );

    • Line 1 :  We call the service to find out about the mailbox.
    • Line 2 :  We are interested in the inbox only, but we could be interested in the calendar. This is where to specify those items.
    • Line 3 :  We only want 10 items…change that number for different results
    • Line 5:  Print out the items, we are only going to show the subject but there are other header items we could show.
  6. Run the program. If everything goes alright then we have gotten the top ten items of the mailbox.

That should get you started. Again check the Getting Started document which the install dropped into the install folder for more examples! 

    5 comments so far

    1. foamy October 20, 2009 2:34 am

      Hi,

      Great article, helped me a lot :)

      I have a question: I’m using the Credentials property to authenticate before using the AutoDiscoverUrl method, but whenever I do, the mailbox that is returned belongs to the user I specified in the Credentials.
      E.g I specify Admin in the Credentials and user@domain.com in the AutoDiscoverUrl method. This will give me the Admin’s Inbox.
      How can I specify an Administrator account and still get some other mailbox based on the e-mail address I enter in the AutoDiscoverUrl method?

      Hope it makes sense. Thanks again :)
      /foamy

    2. omegaman October 20, 2009 3:39 pm

      Yes it does make sense and I wrote about it in my new blog article Tribal Knowledge: EWS C# Extract Alternate Email Address’ Mailbox HTH

    3. foamy October 21, 2009 12:06 am

      Brilliant, that does exacly what I need :D

      You’re a diamond!

    4. Kenneth Scott October 28, 2009 6:05 am

      If we use the EWS API and have to hard-code the Exchange version (ExchangeVersion.Exchange2007_SP1) – is our code going to break when we upgrade to Exchange 2010?

      Thanks-
      Kenneth

    5. omegaman October 28, 2009 3:40 pm

      Update: Yes I verified this with Microsoft it will work. The idea is that 2010 has a version of web services and that it will provide both versions 2010 and 2007 SP1. So older clients will not break

      Good question, I haven’t played with 2010 beta to answer that. I should load it up in an Hyper-V virtual on my server and test. I will investigate…

      But I believe that specifing ExchangeVersion.Exchange2007_SP1 one is simply saying at a minimum that the process want a version of exchange and if the server is 2010 it simply provides web services at that lower version. Hence older clients won’t break.

    Leave a comment

    Please be polite and on topic. Your e-mail will never be published.

    Please visit WP-Admin > Options > Snap Shots and enter the Snap Shots key. How to find your key