In this article i am using Exchange 2007, Server 2008x64 and visual studio 2008. However I expect most versions of forementioned programs will function in a similar way.
Setting up the EWS Urls:
As with most aspects of development, the setup can sometimes take a good chunk of time and research to get right, after a day or so googling and experimenting with various exchange SDK’s I found what I believe to be the best solution for programatically accessing Exchange, The Exchange Web Service or EWS as it is also known.
If you have your exchange server setup correctly; you should be able to use the following to program tools, pages and applications that will enable you to programatically use Microsoft Exchange’s extensive but convoluted abilities.
We need to start by looking at the Exchange management shell this is basically power shell but with an Exchange scope
If your not fammiliar with powershell, it will look rather unusual compared to your standard command prompt, but it’s not to dissimilar.
We are going to use two comands
- Get-WebServicesVirtualDirectory
- we want to run it with the following arguments
- Get-WebServicesVirtualDirectory | select Name, *url* | fl
- This will format the result of the command so that is presented in a far friendly format.Running this should produce a simmilar screen as the one shown below.
- What these properties mean
- Name: This is the virtual directory and the name of the website in which the EWS is running on.
- InternalNLBBypassURL: This will most likely be the same as the internal url, its to do with non load balacing.
- InternalUrl: As the name says ,the URL which is used internally on your localmachine/network.
- ExternalUrl: The URL which is viewable by the outside world, this is the one we will most likely need to setup, as the rest should have been created during your exchange installation.
- Set-WebServicesVirtualDirectory
- (presuming ExternalUrl is empty) We need now to supply the external URL so that we can reference later in visual studio.
- Todo this, we will use the following command, replacing *myoutsidedomain* and EWS (Default Web Site) appropriatly.
- Set-WebServicesVirtualDirectory -Identity “EWS (Default Web Site)” -ExternalUrl “http://*myoutsidedomain*/ews/exchange.asmx”(ignoring line breaks) See screen show below.
- If all these properties are fulfilled as above then the next steps should work fine.
Using Visual Studio C# to interface with exchange
The next stage is to add a web reference to the EWS in Visual Studio, this is relativly simple.
- Create a blank C# forms Visual Studio project(A Web application is just as good, however it is quicker to develop in winforms than asp.net)
- Select Add Service Reference… from the project menu in Visual Studio see here
- The dialog you are presented with allows us to connect straight to the Exhange Web Service. Enter the ExternalUrl (it should end in asmx/wsdl) we added in the above steps into the ‘URL:‘ bar and click go.It is likely you will get some warnings come up, just click okay/yes(ignore them).Visual Studio should enumerate all of the methods for the EWS. Finally Change the web reference name to Exchange and click Add Reference.
- You now have EWS linked to Visual Studio, accessable from the namespace of Exchange.
Authenticating with Exchange
- One of the first things that you will need to do is Authenticate Exchange.
- If you have an invalid SSL certificate, see the override example below.
Override Example:
//This code tells the service point manager to ignore an error caused by a dodgy SSL Certificate.
public static void OverrideCertificateValidation()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertValidate);
}
public static bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors err)
{
return true;
}
