2014-08-28 23:27:53
来 源
中存储网
Exchange邮件服务器
发现在创建没有邮件地址的时候居然报错,但是把环境改到了2007 却通过,通过工具Fiddler,发现差别如下

公司exchange环境升级到了2010 ,于是用以前的单元测试过了一遍,发现在创建没有邮件地址的时候居然报错,但是把环境改到了2007 却通过,通过工具Fiddler,发现差别如下:

以下是代码片段:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2007_SP1" />
<t:TimeZoneContext>
<t:TimeZoneDefinition Id="China Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SaveOnly">
<m:Items>
<t:Contact />
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>

以下是代码片段:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2007_SP1" />
<t:TimeZoneContext>
<t:TimeZoneDefinition Id="China Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SaveOnly">
<m:Items>
<t:Contact>
<t:EmailAddresses />
</t:Contact>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>

  明显多了一个 EmailAddresses 这个字段,经过反编译代码发现

原来现在这个字段已经是个对象了,所以在处理的时候会自动加上Emailaddressses。

怎么办呢?于是我想到一种办法,既然能在Exchange2010的服务器上采用2007的方式访问,而且是正常的,反而用2010 的方式访问不正常。那为了不干扰别的地方,我采用在局部地方采用原来2007 的访问方式,在程序实例化的时候,采用加载两种不同的服务,分别是当前版本和2007版本

代码如下:

以下是代码片段:
    service = new ExchangeService(RequestedServerVersion)
  {
  Url = new Uri(config.EWSServiceUrl),
  Credentials = new NetworkCredential(config.ExchangeAdministrator,
  config.ExchangeAdministratorPassword,
  config.Domain),
  // ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SID, "MailAdmin")
  };
  service2007 = new ExchangeService( ExchangeVersion.Exchange2007_SP1)
  {
  Url = new Uri(config.EWSServiceUrl),
  Credentials = new NetworkCredential(config.ExchangeAdministrator,
  config.ExchangeAdministratorPassword,
  config.Domain),
  // ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SID, "MailAdmin")
  };

 在创建实体的时候均采用2007的方式(因为2010 可以兼容这个创建的方法)

以下是代码片段:
/// <summary>
/// 创建一个联系人实例,Contact实例不能通过new Contact的方式实例化
/// </summary>
/// <returns></returns>
public Contact CreateContactEntity()
{
Contact contact = new Contact(service2007);
return contact;
}

保存的代码被我改成如下:

以下是代码片段:
public string SaveContact2007(Contact data, string account)
{
var service1 = data.Service;
string mailAddress = string.Format("{0}@{1}", account, ConfigService.CreateInstance().MailDomain);
if (service1.ImpersonatedUserId == null || service1.ImpersonatedUserId.Id != mailAddress)
{
service1.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailAddress);
}

data.Save();
return data.Id.UniqueId;
}

这样,修改的方法亦是如此。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。