使用EWS从电子邮件收件箱中搜索包含特定主题的电子邮件

时间:2014-03-12 17:33:14

标签: c# email exchangewebservices exchange-server-2010

我使用EWS升级使用WebDAV查询Exchange Server 2003邮箱的应用程序,新版本将与Exchange Server 2010 SP2一起使用。

我想要排除包含以下搜索字词的主题的电子邮件: " FATS;作业;沙盒:作业"

我查看了MSDN Library: Searching for items in a mailbox by using the EWS Managed API并了解了如何过滤收件人,以及电子邮件是否有附件,但现在我需要更进一步,只阅读上面没有主题的电子邮件(4种不同的字符串排除)

这是我试过的代码:

searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "FATS;Assignment;Sandbox: Assignment")));

但我知道(最后一项)只会找到包含这些字词的项目,如何使用SearchFilter排除这些字词?

1 个答案:

答案 0 :(得分:1)

我在MSDN Library: Filtering on Not by using the EWS Managed API找到答案:

我必须使用 SearchFilter.Not 对象:

searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));

然后我将其添加到集合中:

searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)));

searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));

// add all to the collection...
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());