2014-02-05 38 views
2

我需要获取具有特定类别名称的所有电子邮件,我该怎么做?按分类搜索Exchange Server EWS

现在我有这样的:

var col = new List<SearchFilter>(); 
col.Add(new SearchFilter.ContainsSubstring(ItemSchema.Categories, "Processed")); 
var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, col.ToArray()); 

FindItemsResults<Item> findResults = service.FindItems(
    WellKnownFolderName.Inbox, 
    filter, 
    new ItemView(10) 
); 

但是,这给了我一个Microsoft.Exchange.WebServices.Data.ServiceResponseException,说{"The Contains filter can only be used for string properties."}

我会怎么做呢?

回答

2

从Exchange 2010起,AFAIK类别是一个多值字段,因此它不适用于搜索过滤器。但是,您可以使用AQS搜索类别。下面的代码应该可以做到。

ExchangeService service = GetService(); 

var iv = new ItemView(1000); 
string aqs = "System.Category:Processed"; 
FindItemsResults<Item> searchResult = null; 
do 
{ 
    searchResult = service.FindItems(WellKnownFolderName.Inbox, aqs, iv); 
    foreach (var item in searchResult.Items) 
    { 
     Console.WriteLine(item.Subject); 
    } 
    iv.Offset += searchResult.Items.Count; 
} while (searchResult.MoreAvailable == true); 
+0

非常感谢!你有什么想法如何搜索没有类别“已处理”的电子邮件? – user3182508

+1

@ user3182508:你所要做的就是使用NOT:string aqs =“NOT System.Category:Processed”; –