在 Exchange 中使用 EWS 获取服务配置信息
了解如何从 Exchange 中的 EWS 获取 UM、策略微移、邮件提示和保护规则的服务配置信息。
EWS 应用程序是否适用于统一消息 (UM) 、策略微移、邮件提示或保护规则? 如果是这样,应用程序需要调用 GetServiceConfiguration 操作 来获取所需的服务配置信息。 GetServiceConfiguration 操作返回特定于每个 EWS 功能的配置信息。
注意
EWS 托管 API 无法实现此功能。
表 1. GetServiceConfiguration 操作返回的配置信息
EWS 功能 | GetServiceConfiguration 操作返回... |
---|---|
UM |
|
策略微移 |
|
邮件提示 |
|
保护规则 |
|
代码示例:使用 EWS 获取邮件提示的服务配置信息
下面的代码示例使用 GetServiceConfiguration 操作 请求邮件提示的服务配置信息。 可以通过添加具有不同值的更多 ConfigurationName 元素来请求其他服务配置信息。
private static void GetServiceConfiguration(ExchangeService service, NetworkCredential creds)
{
// XML for the GetServiceConfiguration request SOAP envelope for mail tips configuration information.
const string getServiceConfigurationRequest =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\"\n" +
" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" \n" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
" <soap:Header>\n" +
" <t:RequestServerVersion Version=\"Exchange2013\" />\n" +
" </soap:Header>\n" +
" <soap:Body>\n" +
" <m:GetServiceConfiguration>\n" +
" <m:ActingAs>\n" +
" <t:EmailAddress>user1@contoso.com</t:EmailAddress>\n" +
" <t:RoutingType>SMTP</t:RoutingType>\n" +
" </m:ActingAs>\n" +
" <m:RequestedConfiguration>\n" +
" <m:ConfigurationName>MailTips</m:ConfigurationName>\n" +
" </m:RequestedConfiguration>\n" +
" </m:GetServiceConfiguration>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
// Encoded GetServiceConfiguration operation request.
byte[] payload = System.Text.Encoding.UTF8.GetBytes(getServiceConfigurationRequest);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(service.Url);
request.AllowAutoRedirect = false;
request.Credentials = creds;
request.Method = "POST";
request.ContentType = "text/xml";
Stream requestStream = request.GetRequestStream();
requestStream.Write(payload, 0, payload.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("You will need to parse this response to get the configuration information:\n\n" + responseFromServer);
reader.Close();
responseStream.Close();
}
else
throw new WebException(response.StatusDescription);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
后续步骤
请求服务配置信息后,使用 XmlDocument 类 加载响应 XML,以便可以对其进行分析。 然后,根据你的方案,你可以执行以下操作之一:
使用 GetMailTips 操作 获取客户端应用程序要向用户显示的邮件提示。
如果启用了 UM, 请了解如何通过手机播放邮箱项目 。