Read emails from Exchange Server

Today I am going to write about exchange server. Last week I was working on a project and get a challenge to read emails from a mailbox on exchange server. It’s a new thing for me to read emails from exchange server. After digging out on the web I am able to read folders from a mailbox on exchange server. I thought it’s a good thing topic and let share my findings.
First of all to read emails from Exchange server you need download the SDK provided by Microsoft. It’s freely available and you can download from the following link,

http://www.microsoft.com/en-us/download/details.aspx?id=35371

After downloading and installing the Exchange server API, open a new Console Application in your Visual Studio. I am using Visual Studio 2012.
In the project add the following assembly reference.

using Microsoft.Exchange.WebServices.Data;
using System.Net;
using System.Net.Security;

I created a new method and name is ReadEmail. The first step will is to create an ExchangeService object. And select the Exchange server version you are using. In my case it’s 2010.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

Next we need to provide the user name, password and domain name. You must provide the UserName who is having access to this mailbox.

service.Credentials = new NetworkCredential(UserName, Password, DomainName);

Then we have to provide the Exchange WebService URL.

service.Url = new System.Uri(Exchange WebService URL);

Now we are ready to read emails from the Inbox. Let’s assume I want to read all the emails from my Inbox, I am not going to place any filter. So I created object of FolderId and mention the folder name (WellKnownFolderName.Inbox) that I want to read from Inbox and also provide the email address from where I am going to read emails. As I am not using filter to read emails and so I will add one check to read only 10 emails from the server.

FolderId inbox = new FolderId(WellKnownFolderName.Inbox, new Mailbox(“[email protected]”));
ItemView iv = new ItemView(10);
FindItemsResults<Item> items = service.FindItems(inbox,iv);

Now we are good to go to read emails. So in the next step we will loop through the emails and read its subject and body.

foreach (EmailMessage msg in items){
msg.Load();
Console.WriteLine(“Subject: “ + msg.Subject + “Message Body: “ + msg.Body);
Console.WriteLine();
}

If you note I add a line msg.Load in the loop. This is important to load the message before reading its detail. If you will not load the message the application will throw an error.

You can add filter, so that emails which fulfill the filter criteria will be fetched from the exchange server. Like in the below example I am going to read only those emails which contains “Hello” keyword in there subject line.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.Credentials = new NetworkCredential(UserName, Password, DomainName);
service.Url = new System.Uri(Exchange WebService URL);
FolderId inbox = new FolderId(WellKnownFolderName.Inbox, new Mailbox(“[email protected]”));

 List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, “Hello”));
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

ItemView iv = new ItemView(10);
FindItemsResults<Item> items = service.FindItems(inbox,searchFilter,iv);
foreach (EmailMessage msg in items)
{
msg.Load();
Console.WriteLine(“Subject: “ + msg.Subject + “Message Body: “ + msg.Body);
Console.WriteLine();
}

Similarly you can send emails from Exchange Server. Below example explain how we can send an email and save a copy in the sent Item folder. First we are creating an EmailMessage object and pass the service object to it. And then creating a folder of SentItems, where we are going to save our sent email messages.

EmailMessage EmailMsg = new EmailMessage(service);
FolderId sentItem = new FolderId(WellKnownFolderName.SentItems, new Mailbox(“[email protected]”));             EmailMsg.ToRecipients.Add(“[email protected]”);
EmailMsg.Subject = msg.Subject;
EmailMsg.Body = msg.Body;
EmailMsg.Sender = FromEmail;
EmailMsg.SendAndSaveCopy(sentItem);

To delete an email message we need to call the Delete method of EmailMessage class. And we need to pass the DeletedMode. Like in the below example I am going to delete a message and move that message to deleted folder.

EmailMessage mail = new EmailMessage(service);
mail.Delete(DeleteMode.MoveToDeletedItems);

I attached the source code, hope this will give you a basic overview of reading emails from exchange server. To get more details about exchange server you will find it here.

http://msdn.microsoft.com/library/office/jj220499(exchg.80)

You can download code from here,

ExchangeServerEmail


 

Leave a Reply