Monthly Archives: October 2012

Exchange Web Services Notifications

Something that I have always enjoyed working with is the exchange web services notifications engine.  This process not only allows you to jump in and fetch information from exchange through the exchange web services but it also gives you a nice subscriber model for responding to any exchange events.  This includes things like emails being received in a specified folder, or calendar entry changes, etc.  What I would like to show you is a quick overview of how the exchange notifications can be grabbed and utilized for whatever you so desire.  In my specific example I am grabbing email notifications and handling them with some business logic to assign them to cases in a help desk system.

Exchange Web Services Notifications – Fetching an event

Our first step is to create an exchange web service object that holds the URL to our service:

Dim excExchangeService As New ExchangeService(ExchangeVersion.Exchange2010)
excExchangeService = New ExchangeService(ExchangeVersion.Exchange2010)
excExchangeService.Url = New Uri(_mExchangeURL)

At this point we subscribe to the event that we awant to grab, and run a loop to hit it until we have events:

While True
  Do
    gerResult = plsSubscription.GetEvents()
    For Each ntfEvent As NotificationEvent In gerResult.AllEvents
      If TypeOf ntfEvent Is ItemEvent Then
        ..Handle notification event
      End If
    Next
  Loop While plsSubscription.MoreEventsAvailable
End While

Exchange Web Services Notifications – What do I do with an item once I have it?

Each event will be typed as either an item type specific to the folder or location that you setup your pull to pull from. In my specific example I setup a few timers to listen to email boxes and waited for the events to come in. When they come in I get an email item so I call the EmailMessage.Bind method to bind an Email object and read some information about the email.

A big push in .NET and software development in general is to move away from a service based process that reads every 5 minutes and sends updates and move into more of a reactionary event driven approach.  This is the best of both worlds, as you do not have to have an event driven model but at the same time your timer is not reading every certain time period and pulling in information.  By providing a real-time system that reacts to events we can tightly integrate our applications with the exchange processes.

Exchange Web Services Notifications provides not only a place for you to hook into great events from the exchange process, but an excellent object model for retrieving any information you need about exchange.