Need Quality Code? Get Silver Backed

Exchange Integration using EWS

1stMar

0

by Gary H

Exchange offers a number of ways to interact with the server programmatically. In this post we will be exploring the use of EWSMC (Exchange Web Services Managed Client) to interact with the server.

Getting Started

We begin by acquiring the latest version of the EWS libraries from Microsoft. Install the package, copy the Microsoft.Exchange.WebServices.dll file into a lib folder for your application and reference it.

Connecting

Connecting using EWSMC is a case of creating a new instance of ExchangeService and giving it the URL of your servers EWS endpoint along with valid credentials to access a mailbox. The URL should be in the format of:

https://[YourExchangeServerUrl]/EWS/Exchange.asmx

Adding your credentials is a case of assigning a new NetworkCredential to the Credentials property on your client. Putting this together gives us a utility method like:

protected ExchangeService CreateManagedServiceClient(string username, 
										string password, string domain)
{
	var ews = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
		{
			Url = new Uri("https://[URL]/EWS/Exchange.asmx"),
			Credentials = new NetworkCredential(username, password, 
												domain)
		};

	return ews;
}

Getting Unread Messages

Interacting with the MailBox is done using the FindItems method and providing a search filter. The search filters themselves have a slightly odd method of creation in that you new up inline types from the SearchFilter class. The filter to search for all unread mail looks like:

var allUnreadMailsFilter = new SearchFilter.SearchFilterCollection 
	{ 
		new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false) 
	};
var unreadItems = Client.FindItems(WellKnownFolderName.Inbox, 
									allUnreadMailsFilter, 
									new ItemView(int.MaxValue));

Loading Attachments

The EWSMC does not by default load all of the properties of mail items when it retrieves a message. You need to manually tell it which properties should be populated using the LoadPropertiesForItems method. This method takes a PropertySet which itself calls out the Properties to be loaded. Attachments themselves also come in a variety of types dependant on whether they are files or other attached emails. Taking our previous snippet, if we wanted to load all of our attachments and then act on all File attachments we would use:

foreach (EmailMessage message in unreadItems)
{
	foreach (var attachment in message.Attachments
									.OfType<FileAttachment>())
	{
		using (var ms = new MemoryStream())
		{
			attachment.Load(ms);
			// Do something with the byte[] payload
		}
	}
}

Sending a mail

The final common task we will look at is sending an email. This becomes very easy using EWSMC, we simply create a new EmailMessage, add some recipients, set a subject and body then call SendAndSaveCopy. Assuming that Client mapped onto a valid ExchangeService endpoint we could use a utility method like:

public void SendEmail(string to, string subject, string body)
{
	var msg = new EmailMessage(Client);
	msg.ToRecipients.Add(to);
	msg.Body = body;
	msg.Subject = subject;
	msg.SendAndSaveCopy();
}

That concludes our look at the basic use cases of EWSMC.

C# , Exchange

Comments are Locked for this Post