Home » EWS (Exchange Web Services) – Basics

EWS (Exchange Web Services) – Basics

EWS is a web service that runs on Microsoft Exchange (2007 SP1+) which allows you to use .NET to do some pretty cool things with Exchange, all without needing to use Outlook or write things that use the Outlook Interop (thus consuming an Outlook license).

This guide assumes you are familiar with Windows Explorer (copying files) and Visual Studio (Adding References, Writing Code and Adding Components onto a Windows Form Application).

Code is provided in C#.

The first thing we need to do is to download and install the EWS Managed API, add references in Visual Studio and create a small application just to ensure it works.

Step 1 – Obtain the EWS DLL file(s) needed.
Step 2 – Add References in Visual Studio to use EWS.
Step 3 – Create a small application to test EWS.

Step 1

As of writing, the latest version of EWS is the v2 API – download it from here.

If you have a location where you store custom APIs or shared libraries, then copy the EWS API to it, keeping your libraries/APIs all organised.

To copy the EWS API files, you should first go to %PROGRAMFILES%\Microsoft\Exchange\Web Services\2.0 and copy the following 4 files to your shared library folder.

Microsoft.Exchange.WebServices.Auth.dll
Microsoft.Exchange.WebServices.Auth.xml
Microsoft.Exchange.WebServices.dll
Microsoft.Exchange.WebServices.xml

Note: This guide assumes that you are installing the 64-bit edition of the EWS API on a 64-bit machine. The instructions above will also work if you install a 32-bit edition of the EWS API on a 32-bit machine. If you install the 32-bit edition of the EWS API to 64-bit machine, you will need to use %PROGRAMFILES(x86)% instead of %PROGRAMFILES%.

Step 2

Fire up Visual Studio (this guide uses Visual Studio 2010) and create a new Windows Forms project (it can be any project you like, but the guide uses a Windows Forms project and a target framework version of 4.5).

Firstly, you need to add references to the EWS API – you can do this by right-clicking the project and selecting ‘References’ and adding ‘Microsoft.Exchange.WebServices.dll’ to your project.

In your project, drag a button onto the form and then assign a ‘Click’ event to the button.

Now it’s time to actually code something.
In your project, add the following Reference:

Next you need to set-up authentication, the exchange version you will use, your EWS connection method, your target mailbox and actually connect to EWS.

Inside your button click event – add the following (the comments are safe to paste in and will tell you what it is doing):

 

 

Whilst there are undoubtedly better ways to determine if you can connect to EWS at all, this is the way I’m using in my application as the target inbox is NEVER empty – if anyone has a better way to determine if you can/cannot connect to EWS, please feel free to share.

Contact Me
* indicates required field