ExchangeService class

Represents a binding to Exchange Web Services (EWS).


Namespace:  Microsoft.Exchange.WebServices.Data
Assembly:  Microsoft.Exchange.WebServices (in Microsoft.Exchange.WebServices.dll)

public sealed class ExchangeService : ExchangeServiceBase

Before you can use the EWS Managed API to perform any task, you must create an instance of the ExchangeService class. But the ExchangeService class goes beyond just serving as a starting point for EWS Managed API client development. It has quite a bit of functionality built in.

Table 1:  Ways that you can use the ExchangeService class

If you want to…

Do this

Control authentication

If your users log on to Windows with the same credentials they use to access their Exchange server, set the UseDefaultCredentials property to true to enable your application to connect to the Exchange server without asking the user for a user name and password.

If you need to specify the user name and password, set the Credentials property. The Credentials property uses a WebCredentials object to keep users’ authentication information safe.

Use the Credentials property along with the ImpersonatedUserId property to authenticate as a service account and impersonate a different user in your application. This requires that the service account be given impersonation rights to the user account.

Manage endpoints

Use the AutodiscoverUrl method to automatically find the correct EWS endpoint for your user. Alternatively, if you have the endpoint for your user cached, you can use the Url property to use that endpoint.

Work with groups of items

Use the CopyItems, CreateItems, DeleteItems, MoveItems, and UpdateItems methods to perform bulk operations on multiple items. You can use these methods to handle scenarios such as creating multiple Contact objects to support the bulk import of contacts or marking multiple Task objects complete, all in a single EWS request.

Work with delegates

Use the AddDelegates, GetDelegates, RemoveDelegates, and UpdateDelegates methods to work with delegates.

While we can’t cover everything that you can do with the ExchangeService class here, you can find out more by exploring the methods and properties. Other features of this class that you might be interested in include:

  • Automatic cookie management

  • Client logging for troubleshooting

  • Rules and Automatic Replies (Out of Office) settings management

  • Name resolution against the address book

  • Mailbox search

Now let's see it all in action. The following code example shows you how to set a specific user name and password, discover the EWS endpoint by using Autodiscover, create two new contacts in the user's default Contacts folder, and grant a second user delegate access to the user's Calendar folder.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Security;
using Microsoft.Exchange.WebServices.Data;

static bool RedirectionCallback(string url)
    return url.ToLower().StartsWith("https://");

static void UseExchangeService(string userEmailAddress, SecureString userPassword)
    ExchangeService service = new ExchangeService();

    #region Authentication

    // Set specific credentials.
    service.Credentials = new NetworkCredential(userEmailAddress, userPassword);

    #region Endpoint management

    // Look up the user's EWS endpoint by using Autodiscover.
    service.AutodiscoverUrl(userEmailAddress, RedirectionCallback);

    Console.WriteLine("EWS Endpoint: {0}", service.Url);

    #region Working with groups of items

    // Create two new contacts in the user's default
    // Contacts folder.
    List<Contact> contactsToAdd = new List<Contact>();

    Contact newContact1 = new Contact(service);
    newContact1.GivenName = "Rosetta";
    newContact1.Surname = "Simpson";
    newContact1.PhoneNumbers[PhoneNumberKey.MobilePhone] = "425-555-1234";
    newContact1.EmailAddresses[EmailAddressKey.EmailAddress1] = "";


    Contact newContact2 = new Contact(service);
    newContact2.GivenName = "Barney";
    newContact2.Surname = "Carmack";
    newContact2.PhoneNumbers[PhoneNumberKey.MobilePhone] = "425-555-5678";
    newContact2.EmailAddresses[EmailAddressKey.EmailAddress1] = "";


    ServiceResponseCollection<ServiceResponse> createItemsResponse =
        service.CreateItems(contactsToAdd, WellKnownFolderName.Contacts, null, null);

    if (createItemsResponse.OverallResult != ServiceResult.Success)
        Console.WriteLine("CreateItems returned a non-success response!");
        for (int i = 0; i < createItemsResponse.Count; i++)
            Console.WriteLine("{0}: {1} - {2}", i + 1,
                createItemsResponse[i].ErrorCode, createItemsResponse[i].ErrorMessage);
        Console.WriteLine("CreateItems successfully created 2 contacts in default Contacts folder.");

    #region Working with delegates

    // Add a user as a delegate with Reviewer rights
    // to the user's Calendar folder.
    Mailbox mailbox = new Mailbox(userEmailAddress);

    DelegateUser newDelegate = new DelegateUser("");
    newDelegate.Permissions.CalendarFolderPermissionLevel = DelegateFolderPermissionLevel.Reviewer;

    List<DelegateUser> delegatesToAdd = new List<DelegateUser>();

    Collection<DelegateUserResponse> addDelegateResponse = service.AddDelegates(mailbox, null, delegatesToAdd);

    for (int i = 0; i < addDelegateResponse.Count; i++)
        if (addDelegateResponse[i].Result != ServiceResult.Success)
            Console.WriteLine("Unable to add {0} as a delegate.", 
            Console.WriteLine("    {0}: {1}", addDelegateResponse[i].ErrorCode, 
            Console.WriteLine("Added {0} as a delegate.", 

Any public static ( in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.