Exchange2007提供了EWS Managed API和Web Service 两种形式的开发方式。这里主要讲述Web Service形式的开发方式。
Web Service目前还不支持的包括:
1.文件夹关联信息消息;
2.通过代理访问;
3.支持公共文件夹;
4.管理员级别的函数支持(Exchange管理功能)
5.文件夹访问控制;
6.委托管理;
客户端与EWS通信图
在VS2010中添加Exchange 2007 Web Service
1.在项目中右击Service References -->点击Add Service References-->点击Advanced-->点击Add Web Reference
2.在URL中输入引用地址:https://domain/EWS/exchange.asmx
3.Add Reference
4.在代码中using Namespace.WebServiceName
下面的例子通过编程获取收件箱中文件夹的ID:
static void Main(string[] args) { // 创建Web Service服务 绑定 // ExchangeServiceBinding binding = new ExchangeServiceBinding(); binding.Url = @"https://yourserver/ews/exchange.asmx"; binding.Credentials = new NetworkCredential("username", "password", "domain"); Dictionary<DistinguishedFolderIdNameType, FolderIdType> mapping; // 通过绑定的用户名映射对应的收件箱中文件夹的ID // mapping = GetAllDistinguishedFolderIds(binding); Console.WriteLine("The Id of the inbox is: \r\n" + mapping[DistinguishedFolderIdNameType.inbox].Id); Console.WriteLine(); Console.WriteLine("The Id of sentitems is: \r\n" + mapping[DistinguishedFolderIdNameType.sentitems].Id); }
创建 distinguished的文件夹名映射到文件夹的ID
GetAllDistinguishedFolderIds方法
public static Dictionary<DistinguishedFolderIdNameType, FolderIdType> GetAllDistinguishedFolderIds(ExchangeServiceBinding binding) { // 创建请求 // GetFolderType getFolderRequest = new GetFolderType(); getFolderRequest.FolderIds = GetAllDistinguishedFolderIdsForRequest(); getFolderRequest.FolderShape = new FolderResponseShapeType(); getFolderRequest.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly; // 响应 // GetFolderResponseType response = binding.GetFolder(getFolderRequest); ResponseMessageType[] responseMessages = response.ResponseMessages.Items; Dictionary<DistinguishedFolderIdNameType, FolderIdType> result = new Dictionary<DistinguishedFolderIdNameType, FolderIdType>(); Array enumValues = Enum.GetValues(typeof(DistinguishedFolderIdNameType)); // 遍历每一个消息 for (int folderIndex = 0; folderIndex < responseMessages.Length; folderIndex++) { FolderInfoResponseMessageType folderInfoMessage = responseMessages[folderIndex] as FolderInfoResponseMessageType; FolderIdType folderId = null; if (folderInfoMessage.ResponseClass == ResponseClassType.Success) { // 获取ID folderId = folderInfoMessage.Folders[0].FolderId; } result.Add( (DistinguishedFolderIdNameType)enumValues.GetValue(folderIndex), folderId); } return result; }
检索所有的文件夹
public static BaseFolderIdType[] GetAllDistinguishedFolderIdsForRequest()
{
Array enumValues =
Enum.GetValues( typeof (DistinguishedFolderIdNameType));
BaseFolderIdType[] result = new BaseFolderIdType[enumValues.Length];
for ( int folderIndex = 0 ; folderIndex < enumValues.Length; folderIndex ++ )
{
DistinguishedFolderIdType id = new DistinguishedFolderIdType();
id.Id = (DistinguishedFolderIdNameType)
enumValues.GetValue(folderIndex);
result[folderIndex] = id;
}
return result;
}
输出结果:
上述事例讲述了如何通过引用Exchange Web Serivce,获取Exchange中的一些信息,下节我们将围绕Exchange编程开发这一主题进行讲述。