使用 EWS 托管 API 与 EWS 进行通信Communicate with EWS by using the EWS Managed API

查找有关如何使用 EWS 托管 API 与 Exchange 中的 EWS 进行通信的信息。Find information about how to use the EWS Managed API to communicate with EWS in Exchange.

EWS 托管 API 中的ExchangeService类包含用于设置用户凭据、标识 EWS 终结点、发送和接收 SOAP 消息以及将绑定配置为与 EWS 通信的方法和属性。The ExchangeService class in the EWS Managed API contains the methods and properties that you use to set user credentials, identify the EWS endpoint, send and receive SOAP messages, and configure the binding to communicate with EWS. 您必须先创建ExchangeService类的实例并将其绑定到 EWS,然后才能使用 EWS 托管 API 执行任何任务。Before you can use the EWS Managed API to perform any task, you have to create an instance of the ExchangeService class and bind it to EWS.

在使用用户凭据和 EWS 终结点设置ExchangeService对象之后,引用ExchangeService对象的任何邮箱对象都可以使用以下方法类型与 EWS 进行通信:After you set up an ExchangeService object with user credentials and the EWS endpoint, any mailbox object that references the ExchangeService object can use the following method types to communicate with EWS:

  • ExchangeService 对象方法- ExchangeService对象上不是从基本对象类型继承的所有方法都将调用 EWS。ExchangeService object methods — All the methods on the ExchangeService object that are not inherited from the base Object type make calls to EWS.

  • Exchange 邮箱项目和文件夹类型方法。Exchange mailbox item and folder type methods.

表1。与 EWS 通信的邮箱项目和文件夹类型方法Table 1. Mailbox item and folder type methods that communicate with EWS

MethodMethod 功能What it does 它调用的操作Operations that it calls
LoadLoad
获取项、附件或用户配置对象的属性。Gets properties on an item, attachment, or user configuration object.
GetItem 操作GetItem operation

GetAttachment 操作GetAttachment operation

GetUserConfiguration 操作GetUserConfiguration operation
绑定Bind
使用服务器上的现有项目中的信息填充客户端上的新项目。Populates a new item on the client with information from an existing item on the server.
GetItem 操作GetItem operation
SaveSave
在服务器上保存客户端项目的副本。Saves the copy of the client item on the server.
UpdateItem 操作UpdateItem operation

UpdateFolder 操作UpdateFolder operation

CreateItem 操作CreateItem operation

CreateFolder 操作CreateFolder operation
更新Update
使用在客户端上所做的更改更新服务器。Updates the server with the changes made on the client.

对于项目和文件夹, Update方法使用UpdateItem 操作UpdateFolder 操作For items and folders, the Update method uses the UpdateItem operation and the UpdateFolder operation.
UpdateItem 操作UpdateItem operation

UpdateFolder 操作UpdateFolder operation
删除Delete
删除服务器上的项目。Deletes an item on the server.

对于项目和文件夹, Delete方法使用和DeleteFolder 操作For items and folders, the Delete method uses the and the DeleteFolder operation.
DeleteItem 操作DeleteItem operation

DeleteFolder 操作DeleteFolder operation
CopyCopy
在服务器上创建一个或多个文件夹的副本。Creates a copy of the item or folders on the server.
CopyItem 操作CopyItem operation

CopyFolder 操作CopyFolder operation
MoveMove
移动服务器上的项目或文件夹。Moves items or folders on the server.
MoveItem 操作MoveItem operation

MoveFolder 操作MoveFolder operation

使用 EWS 托管 API 与 EWS 进行通信To use the EWS Managed API to communicate with EWS

  1. 实例化ExchangeService类。Instantiate the ExchangeService class.

     ExchangeService service = new ExchangeService();
    

    备注

    使用空构造函数实例化ExchangeService将创建一个绑定到最新已知版本的 Exchange 的实例。Instantiating ExchangeService with an empty constructor will create an instance that is bound to the latest known version of Exchange. 或者,可以通过指定 version 作为参数来设定特定版本的 Exchange。Alternatively, you can target a specific version of Exchange by specifying version as a parameter. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

  2. 设置向 Exchange 服务器发送请求的用户的凭据。Set the credentials of the user who sends requests to the Exchange server. 如果要使用已通过身份验证的用户的凭据从登录到域的计算机连接到 EWS,请将ExchangeService对象的UseDefaultCredentials属性设置为trueIf you want to connect to EWS 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;
    

    如果不想使用默认用户凭据进行连接,请设置ExchangeService对象的凭据属性以明确指定不同用户的凭据。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. 如果您使用的是 Exchange Online 或 Exchange Online 作为 Office 365 的一部分,您将使用基本身份验证,仅使用用户名和密码。If you are using Exchange Online or Exchange Online as part of Office 365, you'll use basic authentication, with just a user name and password. 若要进行 NTLM 身份验证,域名是必需的。A domain name is required for NTLM authentication.

     // 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");
    

    备注

    如果将UseDefaultCredentials属性设置为true,则将忽略凭据属性的值。If the UseDefaultCredentials property is set to true, the value of the Credentials property is ignored.

  3. 设置 EWS 终结点的 URL。Set the URL of the EWS endpoint. 此 URL 定位客户端访问服务器上的 exchange .asmx 文件。This URL locates the exchange.asmx file on Client Access server.

     // Use Autodiscover to set the URL endpoint.
     service.AutodiscoverUrl("user1@contoso.com");
    

    备注

    虽然您可以显式地将ExchangeServiceUrl属性设置为硬编码的值,但我们建议您改为使用自动发现服务,原因如下: > 自动发现将确定给定用户的最佳终结点(最接近用户邮箱服务器的终结点)。Although you can explicitly set the Url property of the ExchangeService to a hardcoded value, we recommend that you use the Autodiscover service instead, for the following reasons: > Autodiscover determines the best endpoint for a given user (the endpoint that is closest to the user's Mailbox server). 如果部署了新的客户端访问服务器,则 > EWS URL 可能会更改。> The EWS URL might change if new Client Access servers are deployed. 在这种情况下,使用自动发现不需要更改代码。In this scenario, using Autodiscover means no code changes are necessary. > 应显式设置 URL 或调用AutodiscoverUrl,但不应同时进行这两个操作。> You should either set the URL explicitly or call AutodiscoverUrl, but you should not do both.

另请参阅See also