Exchange Web Services Managed API 1.0 大概是在微软在09年10月左右发布的目前国内的资料非常少,国外倒是还有一些比较好的资料。这是一篇入门级的文章,只希望能给大家一个简单的介绍,具体的更详细的大家可以在MSDN里面找。
Exchange在2007之后提供了Exchange Web Service(EWS),它提供了一个访问Exchange资源的全新接口。EWS达到了下面几个目标:
- 提供了Exchange资源的统一访问。
- 内置了与outlook一致的逻辑层(换句话说,用outlook能做的操作,用EWS 也能做到)
- 基于Web Service标准
- 可远程访问
EWS集合了原来WebDAV和CODEX的功能,它提供的相关功能使实现日程安排等操作非常容易。EWS是基于SOAP协议的XML WebService,这使它能够被可以发送HTTPS请求的任何操作系统远程访问。
Exchange2007 SP1提供了一些Exchange2007最初发布所不包含的功能,比如delegate管理,公共文件夹管理,Exchange2010将会包含更多新的EWS功能,包括文件夹关联项的访问,用户配置对象访问(这些对象让开发者可以通过邮箱保存和检索用户配置),Distribution lists的完全控制,增强时区支持。。
EWS是非常高效的Exchange资源访问接口。如果采用自动生成的代理类来访问EWS,比如在使用在Vistual Studio中新增Web Service引用服务的方法,对于一个复杂应用来说,并不是一个好的方法。可能还是要写一大堆的代码,因为那些代理类只是协议的直接映射。且自动生成的代码使用起来非常不方便(比如Vistual Studio 生成的一些晦涩难懂的属性),这些都让自动生成的代理类难以使用和维护。
为了解决这些使用和维护的问题,我们推出了Microsoft Exchange Web Services (EWS) Managed API
最佳的Exchange .NET开发
EWS Managed API是一个完全的面向对象的API,就像.Net Framework 类库一样。它基于EWS XML 协议,提供了非常容易学习,使用和维护的Exchange Web Service .NET开发接口,无论是初学者,还是有丰富经验的开发者都能发现它相对于代理类的优势。
尽管EWS Managed API是一个全新的API(意味着我们要在应用中添加新的程序集),但必须明白一点:它并没有替代EWS 协议。它仅仅为.Net开发者实现了EWS协议。这表示以前的代码还是可以使用的。无论使用最基本XML(比如Javascript程序员使用的),还是采用代理类与EWS通信,以前的应用都是可以继续使用的。EWS协议一直会是Exhange的一项功能,所有在EWS协议中添加的功能,也会在EWS Manage API所有体现。
使用就是如此简单
EWS Manged API 确实比代理类有极大的改进。下面是检索文件夹属性的代码,将会对两种方法进行对比,先是采用代理类,然后是采用EWS Managed API.
用代理类检索文件夹属性:
- GetFolderType request = new GetFolderType();
- request.FolderShape = new FolderResponseShapeType();
- request.FolderShape.BaseShape = DefaultShapeNamesType.AllProperties;
- DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();
- inboxId.Id = DistinguishedFolderIdNameType.inbox;
- request.FolderIds = new BaseFolderIdType[] { inboxId };
- GetFolderResponseType response = serviceBinding.GetFolder(request);
- FolderInfoResponseMessageType responseMessage
- = response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;
- if (responseMessage.ResponseClass == ResponseClassType.Success)
- {
- FolderType inbox = responseMessage.Folders[0] as FolderType;
- }
GetFolderType request = new GetFolderType();
request.FolderShape = new FolderResponseShapeType();
request.FolderShape.BaseShape = DefaultShapeNamesType.AllProperties;
DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();
inboxId.Id = DistinguishedFolderIdNameType.inbox;
request.FolderIds = new BaseFolderIdType[] { inboxId };
GetFolderResponseType response = serviceBinding.GetFolder(request);
FolderInfoResponseMessageType responseMessage
= response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;
if (responseMessage.ResponseClass == ResponseClassType.Success)
{
FolderType inbox = responseMessage.Folders[0] as FolderType;
}
用EWS Managed API检索文件夹属性:
- Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
(呵呵,确实简化了很多代码。。)
EWS Managed API 功能:
下面将体会到使用该API的简单,让我们来看看它的一些功能:
绑定EWS
在开始使用EWS Managed API之前,我们先要初始化一个ExchangeService类的一个实例。代码如下
- using Microsoft.Exchange.WebServices.Data;
- ...
- ExchangeService service = new ExchangeService();
- service.Credentials = new NetworkCredential("name", "pwd", "domain");
- service.Url = new Uri(https://myserver/EWS/Exchange.asmx);
using Microsoft.Exchange.WebServices.Data;
...
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("name", "pwd", "domain");
service.Url = new Uri(https://myserver/EWS/Exchange.asmx);
ExchangeService类有许多有用的方法,比如FindItems, FindAppointments, DeleteItems 和 AddDelegates, 可以通过vs 的职能感知查看相关函数:
EWS Managed API 智能感知
可喜的是:EWS Managed API 也支持Exchange 2007 SP1 ,你可以在ExchangeService的构造函数中指定Exchange的版本。
- ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
可以采用同一个版本的EWS Manged API 指定不同版本的Exchange,它可以同时支持Exchange 2007 SP1和Exchange 2010,当然必须注意如果你指定了Exchange 2007 SP1 版本,与Exchange 2010相关的功能就不能使用了,如果使用了将会抛出异常。
使用 Autodiscover
Autodiscover服务是Exchange2007 和Exchange 2010架构中的一个核心部分。采用Autodiscover,程序可以自动获取到与服务器通信的设置方式,比如Exchange Web Services的URL。
Exchange Web Services Managed API提供了一个内置的Autodiscover客户端API,我们不再需要下载Exchange Server Software Development Kit (SDK),从Autodiscover 示例中拷贝代码。因为现在调用Autodiscover服务就只要一个方法。代码如下:
- ExchangeService service = new ExchangeService();
- service.Credentials = new NetworkCredential("name", "pwd", "domain");
- service.AutodiscoverUrl(someone@contoso.com);
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("name", "pwd", "domain");
service.AutodiscoverUrl(someone@contoso.com);
Exchange 2010 将会提供一个新的基于SOAP的Autodiscover服务,下面代码是采用EWS Managed API的方式
- using Microsoft.Exchange.WebServices.Autodiscover;
- ...
- AutodiscoverService autodiscover = new AutodiscoverService("contoso.com");
- autodiscover.Credentials = new NetworkCredential("name", "pwd", "domain");
- GetUserSettingsResponse response = autodiscover.GetUserSettings(
- "someone@contoso.com",
- UserSettingName.ExternalEwsUrl,
- UserSettingName.InternalEwsUrl);
using Microsoft.Exchange.WebServices.Autodiscover;
...
AutodiscoverService autodiscover = new AutodiscoverService("contoso.com");
autodiscover.Credentials = new NetworkCredential("name", "pwd", "domain");
GetUserSettingsResponse response = autodiscover.GetUserSettings(
"someone@contoso.com",
UserSettingName.ExternalEwsUrl,
UserSettingName.InternalEwsUrl);
处理 项目和文件夹
EWS Managed API提供了项目和文件夹的所有操作功能,包括以面向对象的方式创建,更新,删除。下面的代码展示了这些功能。
在收件箱中创建文件夹
- Folder folder = new Folder(service);
- folder.DisplayName = "My new folder";
- folder.Save(WellKnownFolderName.Inbox);
Folder folder = new Folder(service);
folder.DisplayName = "My new folder";
folder.Save(WellKnownFolderName.Inbox);
创建和保存草稿
- EmailMessage message = new EmailMessage(service);
- message.Subject = "Hello from the EWS Managed API";
- message.Body = "Now that's easy!";
- message.ToRecipients.Add("someone@fabrikam.com");
- message.Save();
EmailMessage message = new EmailMessage(service);
message.Subject = "Hello from the EWS Managed API";
message.Body = "Now that's easy!";
message.ToRecipients.Add("someone@fabrikam.com");
message.Save();
发送邮件且保存副本
message.SendAndSaveCopy();
注意:保存副本不是必须的!
绑定且更新联系人
- Contact contact = Contact.Bind(service, new ItemId("abcdef"));
- contact.CompanyName = "Fabrikam";
- contact.Update(ConflictResolutionMode.AutoResolve);
Contact contact = Contact.Bind(service, new ItemId("abcdef"));
contact.CompanyName = "Fabrikam";
contact.Update(ConflictResolutionMode.AutoResolve);
删除联系人
- contact.Delete(DeleteMode.HardDelete);
contact.Delete(DeleteMode.HardDelete);
创建周期性的会议
- Appointment appointment = new Appointment(service);
- appointment.Subject = "Play tennis";
- appointment.Body = "Let's play tennis for an hour every Saturday at 10AM";
- appointment.Start = new DateTime(2008, 12, 20, 10, 00, 00);
- appointment.End = appointment.Start.AddHours(1);
- appointment.RequiredAttendees.Add("someone@fabrikam.com");
- appointment.Recurrence = new Recurrence.WeeklyPattern(
- appointment.Start.Date,
- 1 /* Every week */,
- DayOfWeek.Saturday);
- appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
Appointment appointment = new Appointment(service);
appointment.Subject = "Play tennis";
appointment.Body = "Let's play tennis for an hour every Saturday at 10AM";
appointment.Start = new DateTime(2008, 12, 20, 10, 00, 00);
appointment.End = appointment.Start.AddHours(1);
appointment.RequiredAttendees.Add("someone@fabrikam.com");
appointment.Recurrence = new Recurrence.WeeklyPattern(
appointment.Start.Date,
1 /* Every week */,
DayOfWeek.Saturday);
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
上面的例子可以看出,每一个项目类型都有专门的类与其对应。这个概念和文件夹类似–每一个文件夹类型都有一个类与其对应。下面的图片展示了项目类和文件夹类的继承关系:
项目类继承关系:
文件夹类继承关系:
使用Response对象
借助Exchange Web Services,Response对象能对项目进行一系列的操作,比如回复消息,接受会议请求。EWS Managed API封装了这些概念,给出了一种非常简单的使用方式。让我们看看下面的代码:
回复消息
- EmailMessage message = EmailMessage.Bind(service, new ItemId("abcd"));
- message.Reply("This is my reply!", true /* replyAll */);
EmailMessage message = EmailMessage.Bind(service, new ItemId("abcd"));
message.Reply("This is my reply!", true /* replyAll */);
发送且保存自定义的回复
- ResponseMessage response = message.CreateReply(true /* replyAll */);
- response.BodyPrefix = "This is my customized reply!"; response.CcRecipients.Add("someone@fabrikam.com");
- response.SendAndSaveCopy();
ResponseMessage response = message.CreateReply(true /* replyAll */);
response.BodyPrefix = "This is my customized reply!"; response.CcRecipients.Add("someone@fabrikam.com");
response.SendAndSaveCopy();
接受会议请求
- Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
- appointment.CancelMeeting();
Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
appointment.CancelMeeting();
取消会议请求
- Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
- appointment.CancelMeeting();
Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
appointment.CancelMeeting();
搜索
EWS Managed API提供了非常完善的搜索功能,代码如下:
显示收件箱的所有子文件夹
- FindFoldersResults findResults = service.FindFolders(
- WellKnownFolderName.Inbox,
- new FolderView(int.MaxValue));
FindFoldersResults findResults = service.FindFolders(
WellKnownFolderName.Inbox,
new FolderView(int.MaxValue));
收件箱中查找前10个重要且包含“API”项目
- ItemView view = new ItemView(10);
- // Return only ten items.
- view.SearchFilter = new SearchFilter.SearchFilterCollection(
- LogicalOperator.And,
- new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
- new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
- FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);
ItemView view = new ItemView(10);
// Return only ten items.
view.SearchFilter = new SearchFilter.SearchFilterCollection(
LogicalOperator.And,
new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);
查找文件夹对象
- Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
- inbox.FindItems(view);
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
inbox.FindItems(view);
创建一个新的搜索文件夹
- SearchFolder searchFolder = new SearchFolder(service);
- searchFolder.DisplayName = "My search folder";
- searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
- searchFolder.SearchParameters.SearchFilter = new SearchFilter.SearchFilterCollection(
- LogicalOperator.And,
- new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
- new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
- searchFolder.Save(WellKnownFolderName.SearchFolders);
SearchFolder searchFolder = new SearchFolder(service);
searchFolder.DisplayName = "My search folder";
searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
searchFolder.SearchParameters.SearchFilter = new SearchFilter.SearchFilterCollection(
LogicalOperator.And,
new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
searchFolder.Save(WellKnownFolderName.SearchFolders);
更多内容
本文件仅仅介绍了EWS Managed API的一小部分功能,需要更多内容,可以到Microsoft Exchange Team Blog 或 Microsoft TechNet Forums: Development 查找。
最后再给大家提供点有用的东西:
EWS Managed API 函数参考:Exchange Web Services Managed API Reference.
EWS Managed API 1.0 程序集: 下载地址(如果地址失效,在Microsoft Download Center搜索”EWS API”就能找到新的下载地址了)