using the exchange web service API from c#

This past week I have been looking at the exchange web service API and how we can inspect Emails within Exchange. If you need to read emails from an inbox then you can very easily and quickly by using the exchange web service API which you can download from here.

I was looking for a way to check a folder for emails and then look at the xml file attachments and then do some work on the contents of the attached files – once done with the contents then mark the email as read and then move the email to an archived folder.

In the following example lets assume we will have emails which will be directed into a folder we specify using a rule, the name of the folder will be stored in the web.config file so we can make this configurable at any point, for arguments sake lets call this folder ExchangeAPIDropFolder.

The following code demonstrates how to go about this:-
[sourcecode language=”csharp”]

private static void CheckEmailFolderForContents()
{
string exchangeUsername = ConfigurationManager.AppSettings["ExchangeUsername"];
string exchangePassword = ConfigurationManager.AppSettings["ExchangePassword"];
string exchangeAutodiscoverUrl = ConfigurationManager.AppSettings["ExchangeAutodiscoverUrl"];
string exchangeAPIDropFolderFolderName = ConfigurationManager.AppSettings["ExchangeAPIDropFolderFolderName"];
string exchangeAPIDropFolderArchivedFolderName = ConfigurationManager.AppSettings["ExchangeAPIDropFolderArchivedFolderName"];

ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
{
Credentials = new WebCredentials(exchangeUsername, exchangePassword)
};

ews.AutodiscoverUrl(exchangeAutodiscoverUrl);

FindFoldersResults folderSearchResults = ews.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));

Folder exchangeExchangeAPIArchivedFolder = folderSearchResults.Folders.ToList().Find(
f => f.DisplayName.Equals(exchangeAPIDropFolderFolderName, StringComparison.CurrentCultureIgnoreCase));

//Set the number of items we can deal with at anyone time.
ItemView itemView = new ItemView(int.MaxValue);

foreach (Folder folder in folderSearchResults.Folders)
{
if (folder.DisplayName.Equals(exchangeAPIDropFolderFolderName, StringComparison.OrdinalIgnoreCase))
{
Folder boundFolder = Folder.Bind(ews, folder.Id);

SearchFilter unreadSearchFilter =
new SearchFilter.SearchFilterCollection(
LogicalOperator.And, new SearchFilter. IsEqualTo(
EmailMessageSchema.IsRead, false));

//Find the unread messages in the email folder.
FindItemsResults<Item> unreadMessages = boundFolder.FindItems(unreadSearchFilter, itemView);

foreach (EmailMessage message in unreadMessages)
{
message.Load();

foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();

MemoryStream ms = new MemoryStream(fileAttachment.Content);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);

//TODO – Process File Contents
}
}

//Mark the message as read and then move it to the Archived Folder
message.IsRead = true;
message.Update(ConflictResolutionMode.AlwaysOverwrite);
message.Move(exchangeExchangeAPIArchivedFolder.Id);
}
}
}
}
[/sourcecode]

Thats it – enjoy and feel free to add comments or ask me about this code.