Home > .NET > Getting all items (email, calendar, contacts, etc.) from Microsoft Exchange server thru C#

Getting all items (email, calendar, contacts, etc.) from Microsoft Exchange server thru C#

Getting all items (email, calendar, contacts, etc.) from Microsoft Exchange server thru C#

Recently I came across a requirement to read email from Microsoft Exchange server. For Exchange server 2007, it comes with a Soap based Web Service interface, through which you can do whatever manipulation you want to perform with your mailbox.

To work with exchange server you need to download and install EWS (Exchange Web Services) managed assemblies from Exchange Web Services Managed API.

Here I’m providing a sample app (in C#) to work with EWS:

1. Create a project in visual studio targeting C#.

2. Add a reference to Microsoft.Exchange.WebServices.dll. Add reference to interface Microsoft.Exchange.WebServices.Data in your code using

using Microsoft.Exchange.WebServices.Data;

3. Add below lines of code to initialize the web service:

ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

// exchangeService.Credentials = new NetworkCredential("{Active Directary ID or UserName}", "{Password}", "{Domain}");

exchangeService.AutodiscoverUrl("{Email Id}");

4. Once the service is up and running you can get the email received in your inbox:

FindItemsResults results = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

foreach (Item item in results.Items)
   Console.WriteLine(item.Subject);

5.Run the program and then you can get the top ten items from Inbox.

Categories: .NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment