Saturday, June 22, 2013

Managing Email With Exchange Web Services

Mark Gerow, director of application development and business process at Fenwick & West Mark Gerow, director of application development and business process at Fenwick & West

We hear a lot today about "activity streams" — those scrolling lists of posts, comments, and "likes" that provide the substance of social media sites and blogs. But inside the enterprise, one activity stream reigns supreme: email; and for most firms email means Exchange. While the centrality of email within the 21st century law firm is assumed, managing its content can be a challenge. Beyond inbox quotas and automated archiving, how can IT help manage this flood of unstructured data in a way that promotes matter team collaboration?

One way is through Exchange Web Services (EWS), the set of services upon which Outlook Web Access (OWA) is based, and which provides all the core tools for finding, moving, updating and deleting email. As a tool for manipulating Exchange content EWS has several advantages over Outlook add-ins, including:

• It is not dependent on the version of Outlook installed on the user's desktop (in fact, a local Outlook client is not required).

• EWS does not need to be run from the user's computer, but can be centralized on a network server.

• With appropriate configuration of Exchange, EWS can manipulate the contents of any user's email and calendars, allowing for the development of centralized, firmwide applications.

In this article I will show how EWS may be used to find emails in an inbox matching a certain criterion, upload those matching emails to SharePoint, and move the original emails to the Outlook "Deleted Items" folder. Through this example you will learn the basics of working with email using EWS.

INSTALLING EWS ON A DEVELOPMENT COMPUTER

One nice thing about EWS is that the web service calls are "wrapped" in a class library that can be referenced in .NET, so it's not necessary to interact directly with the web service per se, only to instantiate objects based on the classes in the library, which can be found here. The version I've worked with successfully is 1.2, although there's a later 2.0 version available for download but as of this writing it appears to still have some bugs.

THE SHAREPOINT "COPY" WEB SERVICE

In addition to EWS, we will use SharePoint's built-in "Copy" service to upload emails to a document library. To use this service you will need to add a web service reference to your Visual Studio solution pointing to the URL: http://[your server name]/[path to site]/_vti_bin/copy.asmx.

GETTING CONNECTED TO YOUR EXCHANGE SERVER

To work with Exchange you need a connection specifying the location of the corresponding EWS instance and authorized user credentials. The following routine will get you connected to the inbox associated with the current application user's credentials:

For the complete code used in this story, see the .cs or .pdf file.

Note: EWS allows you to connect to any individual's inbox given sufficient permissions. Such permissions must be granted by your Exchange administrator. See here for more information.

RETRIEVING THE CONTENTS OF AN INBOX

It's often simpler to manipulate inbox content if it's first copied to a DataTable. The following method, "GetInbox" will read through the first items in an inbox that have a specified string in the subject, adding the data for those emails to a Data Table for later processing.

Click image to enlarge
Click image to enlarge.

RETRIEVING A SINGLE ENTRY BY ITS EXCHANGE ID

Each email message has a unique ID that can be used to retrieve its contents.

GETTING THE EMAIL FILE

To upload an email to SharePoint, we need to get the full email as a byte-array. Note that the "Load( )" method is called with a special parameter to get the "MimeContent". This is necessary in order to make the full byte-array representation of the email message available.

UPLOADING THE EMAIL TO SHAREPOINT

Once the metadata and byte-array contents of the email are available, we can use SharePoint's copy.asmx web service to upload a copy of the email to a document library.

DELETING A MESSAGE FROM THE INBOX

Finally, we want to delete the message after it has been uploaded to SharePoint:

message.Delete(DeleteMode.MoveToDeletedItems);

ALMOST, BUT NOT QUITE!

The selected documents are now in a SharePoint library, but they may not open correctly until a setting has been changed on your IIS server.

Click image to enlarge
Figure 1. Click image to enlarge

The reason is that EWS returns emails in the .EML format used by Exchange, not the .MSG format used by Outlook. In order to instruct Outlook to open these messages, you need to associate the .EML extension with the "application/octet-stream" MIME type. This tells Windows that the .EML file type should launch an application.

Click image to enlarge
Figure 2. Click image to enlarge

You may also need to have individual users associate the .EML document type with Outlook. This can be done by pushing a group policy out to all users, or individually by right-clicking on an .EML file and using the "Open with" option to associate Outlook with that type. See Figure 3.

Click image to enlarge
Figure 3. Click image to enlarge

CONCLUSION

Exchange Web Services, the set of services behind Outlook Web Access, can be a powerful ally in taming the email beast. EWS allows you to search through an inbox and process emails without end-user intervention. It also has an advantage over the Outlook object library in that EWS can run on any computer, does not require Outlook to be installed, and with sufficient permissions, allows you to open any inbox for processing.

A browser or device that allows javascript is required to view this content.

Mark Gerow leads the application development team at Fenwick & West and is responsible for defining and implementing the firm's intranet and extranet strategies using SharePoint technologies. Email: mgerow@fenwick.com.

You must be signed in to comment on an article

Sign In or Subscribe
">

View the original article here

No comments:

Post a Comment