Connecting to EWS by using the EWS Managed API
In the Microsoft Exchange Web Services (EWS) Managed API, the ExchangeService class contains the methods and properties that are used to set user credentials, identify the EWS endpoint, send and receive SOAP messages, and configure the connection with EWS. To perform an operation by using the EWS Managed API, you must connect the ExchangeService class to EWS.
-
Instantiate the ExchangeService class.
Note: Instantiating ExchangeService with an empty constructor will create an instance that is bound to the latest known version of Microsoft Exchange Server. Alternatively, you can target a specific version of Exchange Server by specifying version as a parameter. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
For more information about versioning in the EWS Managed API, see Versioning EWS requests. -
Set the credentials of the user who sends requests to the Exchange server. If you want to connect to Exchange Web Services from a computer that is logged on to the domain, using the credentials of the authenticated user, set the UseDefaultCredentials property on the ExchangeService object to true.
// Connect by using the default credentials of the authenticated user. service.UseDefaultCredentials = true;
If you do not want to connect by using the default user credentials, set the Credentials property on the ExchangeService object to explicitly specify the credentials of a different user.
// Connect by using the credentials of user1 at contoso.com. service.Credentials = new WebCredentials("user1@contoso.com", "password");
You can also specify the credentials of the user by using the user's domain name and password.
// Connect by using the credentials of contoso/user1. service.Credentials = new WebCredentials(“user1”, “password”, “contoso”);
Note: If the UseDefaultCredentials property is set to true, the value of the Credentials property is ignored. -
Set the URL of the Exchange Web Services endpoint. This URL locates the exchange.asmx file on the Exchange server that has the Client Access server role installed.
Note: Although you can explicitly set the Url property of the ExchangeService to a hardcoded value, using the Autodiscover service instead is recommended for the following reasons: -
Autodiscover determines the best endpoint for a given user (the endpoint that is closest to the user's Mailbox server).
-
The Exchange Web Services URL might change if new Client Access servers are deployed. In this scenario, using Autodiscover means no code changes are necessary.
-
Autodiscover determines the best endpoint for a given user (the endpoint that is closest to the user's Mailbox server).
-
Write appropriate error handling code for common search errors.
-
Review the client request XML that is sent to the Exchange server.
-
Review the server response XML that is sent from the Exchange server.
-
Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API. 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.
-
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.
-
Do not include user names and passwords in trace files.
-
Ensure that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.