Setting the Exchange service URL by using the EWS Managed API 2.0
Learn how to set the Exchange service URL to use the EWS Managed API.
Last modified: January 25, 2013
Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010
Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.
To use the Exchange Web Services (EWS) Managed API, you must set the Exchange service URL. You can provide the appropriate service binding manually, or by using the AutodiscoverUrl method. The AutodiscoverUrl method initializes the Url property to the Exchange Web Services URL for the specified email address by calling the Autodiscover service.
We recommend that you use the Autodiscover service because the Autodiscover service determines the best endpoint for a specific user. When you use the Autodiscover service, if the EWS URL changes, your code will not be affected.
To set the Exchange service binding manually
Set the EWS URL. Before you can do this, determine the EWS URL by using the Autodiscover service.
// Create the binding. ExchangeService service = new ExchangeService(); service.Credentials = new WebCredentials("UserName", "password", "domain.contoso.com"); // Set the URL. service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx");
To set the Exchange service binding by using the Autodiscover service
The AutodiscoverUrl method uses the email address to set the ExchangeService endpoint and enables the mailbox user to perform any methods of the ExchangeService proxy classes.
// Create the binding. ExchangeService service = new ExchangeService(); service.Credentials = new WebCredentials("UserName", "password", "domain.contoso.com"); // Set the URL. service.AutodiscoverUrl("User1@contoso.com");
Determine the server endpoints
After you obtain the URL for the Exchange server endpoints, you can communicate with the Exchange mailbox. The Exchange service Url property is similar to the following.
https://computer.domain.contoso.com/EWS/Exchange.asmx
Compiling the code
For information about compiling this code, see Getting started with the EWS Managed API 2.0.
Robust programming
Write appropriate error handling code for common search errors.
Review the client request XML sent to the Exchange server.
Review the server response XML sent from the Exchange server.
Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl method.
Security
Use HTTP with SSL for all communication between client and server.
Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.
Do not include user names and passwords in trace files.
Autodiscover lookups that use HTTP GET to find an endpoint should always prompt for user confirmation; otherwise, they should be blocked.