I can’t recall how many times I have come across a requirement to programmatically read emails from an Exchange Server mailbox and take some action based on the presence of new messages. The component reading the emails can read the mail content, parse its contents and transmit the data to other downstream systems. In this blog I’m going to take a look at one way we can do this.
In my scenario there was a requirement to develop a program to retrieve mails from an Exchange mailbox and, based on a specific criteria, send the email to multiple users based on an distribution list identifier. Listed below are the steps, I intended to follow:
- Use an efficient calling mechanism to poll for new email based on a given criteria
- Read any new email messages
- Transform the received email to an SMTP-friendly message to be sent to a list.
I developed my solution in .Net, using C# as the language of preference, hooking into the Exchange Web Services (EWS) Managed API as the polling endpoint and using simple SMTP endpoints to send mails. Let’s look at the solution in a little more detail.
Prerequisites / Preparation
- Open Visual Studio and create a new Console Application
- Install Exchange Web Services (EWS) Managed API from NuGet Package Manager
- Add reference for System.Configuration (to support application configuration).
Create a connection to a given mailbox
When creating connections to an Exchange mailbox we need to know the version of the source Exchange environment. The code to connect will differ based on the target environment. At time of writing, the EWS managed API supports the following Exchange Server versions:
- Exchange 2007 SP1
- Exchange 2010 (inc. SP1 & SP2)
- Exchange 2013 (inc. SP1)
Use the latest (Exchange 2013 SP1) if you are using this to connect with Exchange Online in Office 365. Below is the code to connect and get a service instance attached to an Office 365 mailbox.
Remember to have a AutodiscoverRedirectionUrlValidationCallback function to enable SSL communication.
The sample below shows how to connect to a local Exchange 2007 environment.
Read email using the created service instance
Once the connection is up and running this is relatively straightforward. The process can be summarised as follows:
- Specify the mailbox folder to read messages from (i.e. inbox)
- Specify the source mailbox email identifier
- Specify any search filters
- Specify the aggregation criteria (OR / AND)
- Specify the maximum number of messages to be retrieved
- Retrieve message identifiers using the service instance.
… and some sample code …
Now we have the message identifiers, but the message body and other basic properties are missing. A second call will need to be made to the server retrieve extended properties for each message. Here is the code for that process:
Prepare the outbound mail for sending
I used a temporary custom object to transform the email from the EWS calls prior to sending. Below is the code used to create the class and the mapping logic.
The code below transforms the temporary mail object into a valid SMTP mail, validates the email identifiers, the mail needs to broadcast to, filters out the invalid mail Ids (if found) and sends the mails in multiple allowed batches.
last but not least.. Send the mail..