This 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
- 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.
- Create your project in Studio, the below example C# code targets a Console Application.
- 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.
- 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.
- 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.
- 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!
#1 by foamy on October 20, 2009 - 2:34 am
Quote
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 by omegaman on October 20, 2009 - 3:39 pm
Quote
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 by foamy on October 21, 2009 - 12:06 am
Quote
Brilliant, that does exacly what I need
You’re a diamond!
#4 by Kenneth Scott on October 28, 2009 - 6:05 am
Quote
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 by omegaman on October 28, 2009 - 3:40 pm
Quote
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.
#6 by Melanie on March 25, 2010 - 7:50 am
Quote
Hi,
When trying to run the code provided above I get the following error:
“An unhandled exception of type ‘Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException’ occurred in Microsoft.Exchange.WebServices.dll
Additional information: The Autodiscover service could not be located.”
Can you help with this please?
Thanks
#7 by omegaman on March 25, 2010 - 8:48 am
Quote
Exchange must be version 2007 with SP1. Is your exchange that version?
You may want to post this question to the Exchange Server Development forum. HTH
#8 by Predrag on April 8, 2010 - 3:05 am
Quote
Hello.
Is there way to get all emails from all accounts in exchange server? Can I do it if I have admin (system) account credentials?
#9 by omegaman on April 9, 2010 - 6:00 am
Quote
I haven’t tried it. Post the question to the MSDN Exchange Development Forum. GL
#10 by Simon on April 16, 2010 - 6:46 am
Quote
Hello!
I’m doing a project where I have to get all unread e-mails from an inbox and I’ve tried to get this thing to work but I encountered a problem quite soon after starting. The error message I’m getting is this:
“The type ‘System.TimeZoneInfo’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.”
The two possibilities to this error I’ve come up with is this, I haven’t got any web reference, I couldn’t get the installation of it to work, or the fact that I’m using Windows XP. Do you know what causes the problem and if you do, is there a way working around it?
Thanks and all the best!
Simon
#11 by omegaman on April 19, 2010 - 9:04 am
Quote
Post it to the EWS Exchange forum. That sounds like a C# issue and not EWS. GL
#12 by Simon on April 20, 2010 - 12:19 am
Quote
Alrite!
Cheers!
#13 by Steve Ronald on May 3, 2010 - 11:33 pm
Quote
Connection to the exchange server to read Calendar,
CalendarFolder myCalendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar);
responds:
ErrorMissingEmailAddress
This error indicates that you specified a distinguished folder ID in the request, but the account that made the request does not have a mailbox on the system. In that case, you must supply a Mailbox sub-element under DistinguishedFolderId.
I haven’t a clue how to apply Mailbox sub-element under DistinguisedFolderId
#14 by omegaman on May 4, 2010 - 4:56 am
Quote
Post the question to the MSDN Exchange Development Forum. GL
#15 by McKinney on June 8, 2010 - 3:36 pm
Quote
And here I was thinking I was going to have to break down and use MAPI. Thanks for this article!
#16 by ouTThere on June 10, 2010 - 7:06 pm
Quote
wow, I wish I found this a couple months ago.
I spent hours getting MAPI to work
great stuff
#17 by vikram on July 5, 2010 - 6:22 am
Quote
Hi,
I am doing a project in which I have to read all the bounce e-mail messages sent to a user. I am using EWS web reference from the IIS where Exchange 2007 is deployed. PLease let me know how to send the search to fetch the bounce e-mails which contains some attachment. Actually i want to get the name of the attachment file from the bounce e-mail, update the database that this file ie not sent and delete the found e-mails. please help me.
#18 by SeeeD on July 5, 2010 - 6:25 am
Quote
Thank you so much for the tutorial =)
I’ve lost so much time at the Exchange SDK.
But one thing:
I do not use this method:
service.AutodiscoverUrl( “First.Last@MyCompany.com” );
It needs around 10 seconds, to get the URL.
I’ve saved the URI and wrote it into the URL-Property.
#19 by rajen shrestha on July 8, 2010 - 1:08 am
Quote
How can I move to different folder, out of mail server folder or delete attachement from the exchange mail server?
thanks
#20 by Michael on July 22, 2010 - 4:11 pm
Quote
What if the in box is full?
#21 by OmegaMan on July 23, 2010 - 2:50 pm
Quote
The process can still extract emails…it doesn’t manage the box either by adding or deleting.