by June 1, 2010 0 comments

A lot of companies rely on Microsoft Exchange to provide their messaging and
collaboration services — such as email, scheduling, calendaring, contacts, tasks
and other management tools. With the use of Outlook Web Access and MS Outlook,
users can be extremely productive in what they do. However, it is sometimes
necessary to be able to perform some other messaging or collaboration tasks that
are not available directly within the interface of either of these two software.
In such a case a custom software can be built that uses Exchange to process the
requirement. This article takes a look at a new method to do this. Traditionally
to use mail from code you need to use things like SMTP, CDOSYS, IMAP commands
and stuff. However, with more and more companies moving to Exchange which uses
MAPI, you need to use a different method to do this. MAPI not only gives access
to mail, but all the other features of Exchange as well. But one of the sad
things was using MAPI was quite hard — till now. Microsoft has released a new
managed API called Exchange Web Services (EWS) as part of Exchange 2007 SP1 and
Exchange 2010. Although the API is the same for both, there are some extra
features that are possible only when using Exchange 2010. We will look at some
simple examples of using different features of Exchange with this.

Direct Hit!

Applies To:.NET developers
Learn to use Exchange Web Services to
enhance messaging and collaboration

Primary Link:
Search Engine Keywords: EWS

Installing EWS Managed API
The first thing to do is to of course get the API into your application. For
this, download the appropriate (32-bit/64-bit) version of EWS Managed API v 1.0
from the Microsoft Download Center. Install the pack while noting down the
installation folder. Now fire up VS2008 or VS2010 and create a new project — say
a Windows application. First off, add a new reference to the Microsoft.
Exchange. WebServices.dll file found in the installation folder. Then in the
code, add the following statement:


In this window we add a reference to the EWS API DLL in the project.

using Microsoft.Exchange.WebServices.Data;

Sending mail
Now drop in a textbox(for the email address) and a rich text box (for the
emailbody) and a button to send emails. In the button’s event handler, add the
following code:
ExchangeService service = new ExchangeService();
// Use when machine is IN Domain
service.UseDefaultCredentials = true;
// Use when machine is NOT in Domain
service.Credentials = new WebCredentials("", "password");
service.Url = new
// Create an e-mail message
EmailMessage message = new EmailMessage(service);
// Add properties to the e-mail message.
message.Subject = "Sending from C# Code";
message.Body = richTextBox1.Text;
// Send the e-mail message and save a copy in the default Sent Items folder


Sending an email from code and receiving it correctly.

The code creates a new instance of the EWS. Depending on whether the machine
you will run the application on is in the same domain as the Exchange server or
not, you will need to choose between two sets of code for credentials and the
URL. The AutodiscoverUrl is preferred, but you can code in the URL as well as
shown. The nice thing about using Exchange for sending mails is that it saves
copies on the server — making even sent items completely accessible from
anywhere — something that POP/SMTP combinations simply cannot do.

Creating appointments and meetings
Exchange also has a feature where you can create an appointment for yourself
or invite others to create it as a meeting. The calendar for all who accept gets
automatically blocked for that meeting. With EWS it is fairly easy to setup both
of these. For this sample, drop in a DateTimePicker control (for getting the
start date and time for the appointment) and a textbox (for the duration) and a
button (to submit it). Add the following code to the button’s click event:

This is the interface of the application window, where you can see the
appointment created.

Appointment appointment = new Appointment(service);
appointment.Subject = "My Appointment";
appointment.Body = richTextBox1.Text;
appointment.Start = dateTimePicker1.Value;
appointment.End =

The last line (SendToNone) basically means that we’re saving this appointment
for ourselves. When this code runs, it Sending an email from code and receiving
it correctly.

will create an appointment for the user in the specified date and time and
duration.A small difference can be done for creating the meetings.Simply allow
the entry of other email addresses to invite attendees and add them to the list
as shown: Appointment meeting = new

Appointment(service);meeting.Subject = "My Meeting";
meeting.Body = richTextBox1.Text;
meeting.Start = dateTimePicker1.Value;
meeting.End =
meeting.Location = "Conf Room #1";
string[] invitees = textBox1.Text.Split(‘;’);
for(int i=0; i<=invitees.Length-1; i++)

This is the application interface window, where you can see the Meeting
that was created.

The last line here again shows the difference — this time Exchange sends an
invite to all the attendees and saves a copy locally.

EWS Managed API is very powerful and can use all of Exchange’s
functionalities — such as working with contacts, delegation of inbox, folders,
searching and filtering, sync routines and many other processes. The API SDK
gives a good look into what you can do. The advantage is that applications
written with this no longer have a dependency on having a particular version of
MS Outlook installed and open. If you are working with applications that need to
do messaging and collaboration while on a network that uses Exchange, the EWS
Managed API is the best choice you can use to do this. As it works even better
with Exchange 2010, you can be sure of future proofing your application as well.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.