下表汇总在.net 环境中 Exchange api 支持策略:
API | DLL 名称 | 托管代码支持策略 |
System.Web.Mail | s | 支持 |
WebDAV | 支持 | |
WMI | 支持 | |
CDOSYS | CDOSYS.DLL | 支持,但是 System.Web.Mail 建议 |
CDOEXM | CDOEXM.DLL | 通过使用 COM 互操作程序集支持 |
EXOLEDB | 通过使用 COM 互操作程序集支持 | |
CDOEX | CDOEX.DLL | 支持通过使用一个 COM 互操作程序集 |
CDO 1.2 x | CDO.DLL | 不支持 |
CDONTS | CDONTS.DLL | 不支持 |
MAPI | MAPI32.DLL | 不支持 |
ESE 备份 API | ESEBCLI2.DLL | 不支持 |
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;
}
用EWS Managed API检索文件夹属性:
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
(呵呵,确实简化了很多代码。。)
下面将体会到使用该API的简单,让我们来看看它的一些功能:
在开始使用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);
ExchangeService类有许多有用的方法,比如FindItems, FindAppointments, DeleteItems 和 AddDelegates, 可以通过vs 的职能感知查看相关函数:
EWS Managed API 智能感知
可喜的是:EWS Managed API 也支持Exchange 2007 SP1 ,你可以在ExchangeService的构造函数中指定Exchange的版本。
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([email protected]);
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(
"[email protected]",
UserSettingName.ExternalEwsUrl,
UserSettingName.InternalEwsUrl);
EWS Managed API提供了项目和文件夹的所有操作功能,包括以面向对象的方式创建,更新,删除。下面的代码展示了这些功能。
在收件箱中创建文件夹
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("[email protected]");
message.Save();
发送邮件且保存副本
message.SendAndSaveCopy();
注意:保存副本不是必须的!
绑定且更新联系人
Contact contact = Contact.Bind(service, new ItemId("abcdef"));
contact.CompanyName = "Fabrikam";
contact.Update(ConflictResolutionMode.AutoResolve);
删除联系人
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("[email protected]");
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 */);
发送且保存自定义的回复
ResponseMessage response = message.CreateReply(true /* replyAll */);
response.BodyPrefix = "This is my customized reply!"; response.CcRecipients.Add("[email protected]");
response.SendAndSaveCopy();
接受会议请求
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));
收件箱中查找前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- findResults = service.FindItems(WellKnownFolderName.Inbox, 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);
出处:http://lloydsheng.com/2010/03/exchange-webservice-managed-api.html