Exchange Web Services (2007) – Part One

I was recently asked to create a windows service to interface with the client’s Exchange Server 2007. As I had never done that, I did some research and came across the Exchange Web Services. Basically, Exchange servers 2007 and later expose several web services to enable 3rd party tie-ins to the data. In my case, the client wanted all pdf’s in a certain folder to be downloaded along with the original email as they arrived.

It broke down into the following steps:

  • Reference the Web Service
  • Connect to the Exchange server
  • Start a subscription to the exchange service
  • Polling the exchange server
    • On poll, if the exchange servers reports new events, get items
    • Get attachments for items
    • Save attachments to a local folder

Referencing the Web Service

In order to use the Exchange Web Services, you must create a Web Reference that points to your Exchange Server. Right click on your project and select Add Service Reference from the context menu. Now, since I’m working with 2007, I have to do a few extra steps to make it work correctly – just adding a regular service reference does not work.

Click the Advanced button on the bottom left of the popup, then click the Add Web Reference button on the bottom left of that popup. Now you can actually enter the address of the Exchange Service:

Connecting to the Exchange Server

To connect to the Exchange Service, you create an instance of the ExchangeServiceBinding class, providing it with the URL and login credentials for the Exchange Server.

ServiceBinding = new ExchangeServiceBinding();
ServiceBinding.Url = Url; //
ServiceBinding.Credentials = new NetworkCredential(Username, Password, Domain);

Starting a Subscription

After setting up the ServiceBinding object, in this case I wanted to create a subscription so that I could get updates as messages came in to the Exchange server. There are two types of subscriptions – push and pull. A push subscription requires an additional service to be running that the Exchange server can call as subscribed events occur. As my application did not require live updates, I went with a pull subscription.

Using a pull subscription, you periodically request any new subscribed to events that have occurred on the server. The events are specified when setting up the subscription. The folders you are paying attention to are also specified during this time.

SubscribeType request = new SubscribeType();

// Setup the request:
// Create a PullSubscription object
PullSubscriptionRequestType subscription = new PullSubscriptionRequestType();

// Indicate to what events to subscribe
subscription.EventTypes = new NotificationEventTypeType[1];
subscription.EventTypes[0] = NotificationEventTypeType.NewMailEvent;

BaseFolderType folder = GetFolderByPath(ServiceBinding, folderPath);

// And on which folder to subscribe for these events.
subscription.FolderIds = new BaseFolderIdType[1];
subscription.FolderIds[0] = folder.FolderId;
subscription.Timeout = 5;

request.Item = subscription;

Next, we will actually make the Subscribe request on the ServiceBinding object created previously, then check to make sure it succeeded.

// Call the Subscribe EWS method
SubscribeResponseType response = ServiceBinding.Subscribe(request);

// Extract the first response message that contains the information we need. This
SubscribeResponseMessageType responseMessage = response.ResponseMessages.Items[0] as SubscribeResponseMessageType;

this.ThrowOnError("CreatePullSubscription", responseMessage);

As this post is already getting a bit long, I’m going to break it up over a few posts. Hopefully this contained some information that helped you understand how to connection to an Exchange Web Server. The next post will cover getting notifications from the ServiceBinding.

Leave a Reply

Your email address will not be published. Required fields are marked *