Today I’m starting a litle tutorial about how to enable communication with Exchange 2007 from a C# application. I’ll write on how to make entries to user calendar, add and modificate tasks, set outlook out of office assistant and so on. I’m doing it mainly for me to have one place where I can find all useful code, but I’ll be very happy if someone will find it helpful.

First step to use Exchange functionality is adding to Your project an EWS(Exchange Web Service) reference. For example: if You have an exchange e-mail server under address https://mail.company.com, then url for You web reference should look like this https://mail.company.com/EWS/Services.wsdl. When You finish, You should import newly created namespace to Your *.cs file.

Exchange Service Binding – this is a proxy class that contains all the methods and properties which You can use. Creating ESB object is very simple. Just follow this code:


ExchangeServiceBinding esb = new ExchangeServiceBinding();//Create new object
 esb.Credentials = CredentialCache.CurrentCredentials;//Specify credentials
 esb.Url = "https://mail.company.com/EWS/exchange.asmx";//Specify the URL endpoint from which ESB will receive and send data

Creating above objcet will result in ability to manipulate my on mailbox. But what if You want to f.e. write an event in someone’s else calendar? To accomplish this task You have to use impersonation and grant domain user special permissions which gives him privileges to enter every mailbox on the server. Details on how to do this You will find at the bottom of this post.
To Impersonate ESB use code below:


ExchangeServiceBinding esb = new ExchangeServiceBinding();
 esb.Credentials = new NetworkCredential(login, password, domain);//where login, password and domain are properties of user with special privileges
 esb.Url = "https://mail.company.com/EWS/exchange.asmx";

 ExchangeImpersonationType exExchangeImpersonation = new ExchangeImpersonationType();
 ConnectingSIDType csConnectingSid = new ConnectingSIDType();
 csConnectingSid.PrimarySmtpAddress = userMail;//Mail address of the user whose mailbox we want to modificate(f.e. tomasz.rabinski@company.com
 exExchangeImpersonation.ConnectingSID = csConnectingSid;
 esb.ExchangeImpersonation = exExchangeImpersonation;

With ESB object created as above we can manipulate every mailbox in our Exchange server so use it wisely! Part 2 of this tutorial will be focued on how to add and modify appointments in outlook calendar.

Impersonation in Exchange Server

Setting the Service Account Can Impersonate

1.       Go AD Sites and Services

2.       Select Show Services Node from view

3.       Select Services -> MS Exchange -> First Organization -> Administrative Groups -> Exchange Administrative Groups -> Servers -> Server Name

4.       Select Properties -> Security

5.        Add Service User Account

6.       Check “Exchange Web Services Impersonation” Permission

 

Setting all Mail boxes can be impersonated

 

1.       Go AD Sites and Services

2.       Select Show Services Node from view

3.       Select Services -> MS Exchange -> First Organization -> Administrative Groups -> Exchange Administrative Groups -> Servers -> Server Name -> Information Store -> First Storage Group -> Mailbox Database

4.       Select Properties -> Security

5.        Add Service User Account

6.       Check “Allow Impersonation to Personal Exchange Information” Permission