EWS编程问题二:如何利用EWS读取Exchange里面相应用户的未读邮件数量(解决)
2010-10-11 13:04 努力学习的小熊 阅读(2271) 评论(0) 编辑 收藏 举报最近几个项目都碰到如何读取Exchange中,登录域用户的未读邮件数量问题,查了一些文章这个问题终于解决掉。现在将源代码放在这里。首先新建一个项目,控制台应用程序,WinForm或者WebPart,网站都可以。然后我们添加Exchange的WebService引用,这里可以完成很多对Exchange邮箱的操作,可以分别读取相应文件夹里面的东西,甚至是邮件的内容等等,当然,这肯定是需要用户的用户名和密码才能完成,不然这个Exchange就不安全了,O(∩_∩)O~
在添加Exchange的WebService时有个需要注意的问题,不然你是不能找到ExchangeServiceBinding这个类的。请参考这篇文章。剩下的代码如下:
调用部分的代码:
代码
ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
ExchangeServiceBinding service = new ExchangeServiceBinding ();
service.RequestServerVersionValue = new RequestServerVersion();
service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
service.EnableDecompression = true;
service.Credentials = new NetworkCredential("username", "password", "domain");
service.Url = @https://192.168.100.11/EWS/exchange.asmx;
int merCurrentCount = GetUnreadFolderItemsCount(service, "Inbox");
ExchangeServiceBinding service = new ExchangeServiceBinding ();
service.RequestServerVersionValue = new RequestServerVersion();
service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
service.EnableDecompression = true;
service.Credentials = new NetworkCredential("username", "password", "domain");
service.Url = @https://192.168.100.11/EWS/exchange.asmx;
int merCurrentCount = GetUnreadFolderItemsCount(service, "Inbox");
涉及到的两个方法:
代码
public int GetUnreadFolderItemsCount(ExchangeServiceBinding service, string folderName)
{
int unReadCount = -1;
// Identify the folder properties to return.
FolderResponseShapeType properties = new FolderResponseShapeType();
PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
PathToUnindexedFieldType[] ptufts = new PathToUnindexedFieldType[1] { ptuft };
properties.AdditionalProperties = ptufts;
properties.BaseShape = DefaultShapeNamesType.AllProperties;
// Form the get folder request.
BaseFolderIdType p_folder = FindFolderID(service, folderName);
GetFolderType request = new GetFolderType();
request.FolderIds = new BaseFolderIdType[1] { p_folder };
request.FolderShape = properties;
// Send the request and get the response.
GetFolderResponseType response = service.GetFolder(request);
ArrayOfResponseMessagesType aormt = response.ResponseMessages;
ResponseMessageType[] rmta = aormt.Items;
foreach (ResponseMessageType rmt in rmta)
{
if (rmt.ResponseClass == ResponseClassType.Error)
{
throw new Exception(rmt.MessageText);
}
else
{
FolderInfoResponseMessageType firmt;
firmt = (rmt as FolderInfoResponseMessageType);
BaseFolderType[] folders = firmt.Folders;
foreach (BaseFolderType rfolder in folders)
{
if (rfolder is FolderType)
{
FolderType myFolder;
myFolder = (rfolder as FolderType);
if (myFolder.UnreadCountSpecified)
{
unReadCount = myFolder.UnreadCount;
}
}
}
}
}
return unReadCount;
}
public static FolderIdType FindFolderID(ExchangeServiceBinding service, String folderName)
{
DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();
objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;
FindFolderType requestFindFolder = new FindFolderType();
requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };
requestFindFolder.FolderShape = new FolderResponseShapeType();
requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
//Search filter definition
requestFindFolder.Restriction = new RestrictionType();
#region Contains expression
ContainsExpressionType objContainsExpression = new ContainsExpressionType();
objContainsExpression.ContainmentMode = ContainmentModeType.FullString;
objContainsExpression.ContainmentModeSpecified = true;
objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
objContainsExpression.ContainmentComparisonSpecified = true;
PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();
objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
objContainsExpression.Item = objFieldFolderName;
objContainsExpression.Constant = new ConstantValueType();
objContainsExpression.Constant.Value = folderName;
#endregion Contains expression
requestFindFolder.Restriction.Item = objContainsExpression;
FindFolderResponseType objFindFolderResponse =
service.FindFolder(requestFindFolder);
if (objFindFolderResponse.ResponseMessages.Items.Length == 0)
return null;
foreach (ResponseMessageType responseMsg in
objFindFolderResponse.ResponseMessages.Items)
{
if (responseMsg.ResponseClass == ResponseClassType.Success)
{
FindFolderResponseMessageType objFindResponse =
responseMsg as FindFolderResponseMessageType;
foreach (
BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)
{
return objFolderType.FolderId;
}
}
}
return null;
}
{
int unReadCount = -1;
// Identify the folder properties to return.
FolderResponseShapeType properties = new FolderResponseShapeType();
PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
PathToUnindexedFieldType[] ptufts = new PathToUnindexedFieldType[1] { ptuft };
properties.AdditionalProperties = ptufts;
properties.BaseShape = DefaultShapeNamesType.AllProperties;
// Form the get folder request.
BaseFolderIdType p_folder = FindFolderID(service, folderName);
GetFolderType request = new GetFolderType();
request.FolderIds = new BaseFolderIdType[1] { p_folder };
request.FolderShape = properties;
// Send the request and get the response.
GetFolderResponseType response = service.GetFolder(request);
ArrayOfResponseMessagesType aormt = response.ResponseMessages;
ResponseMessageType[] rmta = aormt.Items;
foreach (ResponseMessageType rmt in rmta)
{
if (rmt.ResponseClass == ResponseClassType.Error)
{
throw new Exception(rmt.MessageText);
}
else
{
FolderInfoResponseMessageType firmt;
firmt = (rmt as FolderInfoResponseMessageType);
BaseFolderType[] folders = firmt.Folders;
foreach (BaseFolderType rfolder in folders)
{
if (rfolder is FolderType)
{
FolderType myFolder;
myFolder = (rfolder as FolderType);
if (myFolder.UnreadCountSpecified)
{
unReadCount = myFolder.UnreadCount;
}
}
}
}
}
return unReadCount;
}
public static FolderIdType FindFolderID(ExchangeServiceBinding service, String folderName)
{
DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();
objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;
FindFolderType requestFindFolder = new FindFolderType();
requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };
requestFindFolder.FolderShape = new FolderResponseShapeType();
requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
//Search filter definition
requestFindFolder.Restriction = new RestrictionType();
#region Contains expression
ContainsExpressionType objContainsExpression = new ContainsExpressionType();
objContainsExpression.ContainmentMode = ContainmentModeType.FullString;
objContainsExpression.ContainmentModeSpecified = true;
objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
objContainsExpression.ContainmentComparisonSpecified = true;
PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();
objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
objContainsExpression.Item = objFieldFolderName;
objContainsExpression.Constant = new ConstantValueType();
objContainsExpression.Constant.Value = folderName;
#endregion Contains expression
requestFindFolder.Restriction.Item = objContainsExpression;
FindFolderResponseType objFindFolderResponse =
service.FindFolder(requestFindFolder);
if (objFindFolderResponse.ResponseMessages.Items.Length == 0)
return null;
foreach (ResponseMessageType responseMsg in
objFindFolderResponse.ResponseMessages.Items)
{
if (responseMsg.ResponseClass == ResponseClassType.Success)
{
FindFolderResponseMessageType objFindResponse =
responseMsg as FindFolderResponseMessageType;
foreach (
BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)
{
return objFolderType.FolderId;
}
}
}
return null;
}
参考文章:Get the Unread count of the Inbox of Exchange 2010/2007, using web services