Getting started with EWS Managed API SDK
EWS Managed API SDK is an easy way to interact with Exchange Web Services from the .Net platform. Exchange web services provides an interface to work with a Exchange server mailbox. Some of the common tasks that can be performed include:
- Searching for specific emails within a folder
- Send emails
- Respond to emails using replies and forwards
- Working with calendar, contacts and availability information
- Work with inbox rules
- Use extended properties on an EmailMessage
To understand EWS better, let us take a scenario, where we want to log information about undelivered emails (NDR) in the database. The logic for such an operation looks simple:
- Search for NDRs within a time interval
- Parse NDRs for relevant information
- Log relevant information into database tables
In this post, we will look at steps 1 and 2. We will ignore step 3 as it is standard stuff.
To start working with EWS, we need to know the EWS endpoint (URL). The endpoint should be discovered using the Autodiscover service (another URL). The Autodiscover service itself should be discovered using either a SCP Lookup in active directory or using the steps outlined in the following article: http://msdn.microsoft.com/en-us/library/ee332364. With EWS Managed API, this boils down to few lines of code:
ExchangeService svc = new ExchangeService();
svc.Credentials = new System.Net.NetworkCredential(userName, password, domain);
svc.AutoDiscoverUrl("vijay@contoso.com");
I maybe slightly getting ahead. So, where is this ExchangeService class? ExchangeWebServices is the name of the NuGet library package that has to be downloaded. This has an assembly – Microsoft.Exchange.WebServices.dll. ExchangeService class is located within this assembly under a namespace – Microsoft.Exchange.WebServices.Data.
To get a list of NDRs within a time interval, we have to query the mailbox. ExchangeService.FindItems() is the method that provides a list of items that match the search criteria. The search criterias include:
- Email created time is within the time interval.
- The ItemClass of email message is REPORT.IPM.Note.NDR
// Search for emails within Inbox
FolderId folderId = new FolderId(WellKnownFolderName.Inbox);
// Search for a max of 100 emails, return only the ID of the email
ItemView itemView = new ItemView(100);
itemView.PropertySet = BasePropertySet.IdOnly;
// Apply search filters
SearchFilter sf1 = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeCreated, fromTime);
SearchFilter sf2 = new SearchFilter.IsLessThan(ItemSchema.DateTimeCreated, toTime);
SearchFilter sf3 = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "REPORT.IPM.Note.NDR");
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter[] { sf1, sf2, sf3 });
// Search for items
FindItemsResults<Item> results = svc.FindItems(folderId, searchFilter, itemView);
There are a few terms that are used. A folder is the same as the folder that we have in a mailbox – Inbox, Outbox, Sent Items etc. A Item is a base class for EmailMessage and other classes that represent an item within the folder. Item has many properties. While finding items, we can retrieve specific properties of the Item by specifying a property set. PropertySet.IdOnly retrieves items with only the ID.
The last piece of the code involves getting some relevant information from the non delivery report.
// Get all properties of email message
PropertySet pset = new PropertySet(BasePropertySet.FirstClassProperties);
// Get the body in text
pset.RequestedBodyType = Microsoft.Exchange.WebServices.Data.BodyType.Text;
// Do an Item.Bind() to retrieve the Item
emailMessage = Item.Bind(svc, item.Id, pset) as EmailMessage;
// Load the attachment in the NDR
ItemAttachment itemAttachment = emailMessage.Attachments[0] as ItemAttachment;
if (itemAttachment != null)
{
// load the item attachment with the body as text
itemAttachment.Load(Microsoft.Exchange.WebServices.Data.BodyType.Text, null);
}
The above code is very straight-forward. We are getting the email message using the ID retrieved in the Find(). We are setting the PropertySet to retrieve all properties. PropertySet also allows the body to be retrieved in text format (default is HTML). We are getting the only attachment in the NDR which is an ItemAttachment. An ItemAttachment is different than FileAttachment as ItemAttachment points to another Item. The attachment in NDR is usually the original sent item. We can use the various properties in the EmailMessage and ItemAttachment.Item to log relevant data to the database.
As you can see, working with EWS Managed API SDK is very simple. Please comment if you have any questions on EWS Managed API SDK.


Recent Comments