searchFilter与EWS FindItems方法调用无法正常工作

时间:2014-03-21 13:08:31

标签: c# email exchangewebservices searchfiltercollection

我使用SearchFilter集合来限制使用EWS将请求返回到Exchange 2010邮箱的电子邮件。

连接服务和打开邮箱没问题。

问题是我的searchFilter被忽略了,请求将所有电子邮件返回给EWS。

这是我的代码:

static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(@"bbtest@bocuk.local");

// Find all items where the body contains "move reports".
//string qstring = "Body:\"move reports\"";

// Identify the item properties to return.
//view.PropertySet = new PropertySet(BasePropertySet.IdOnly,
//ItemSchema.Subject);

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);

List<SearchFilter> searchFilterCollection = new List<SearchFilter>();

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")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sandbox: Assignment")));

SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

ItemView view = new ItemView(100);

string sAttachmentPath = "C:\\Dev\\EWSHelloWorld\\attachments\\";

// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

foreach (EmailMessage email in results)
// looping through all the emails
{

System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);

.... code removed for brevity!

因此,根据我对searchFilter的理解,只应返回带有附件的未读电子邮件,并且没有 FATS Sandbox:Assignment 作为主题。

但这不起作用,对EWS的请求只是返回所有电子邮件。

我做错了什么?

1 个答案:

答案 0 :(得分:7)

菲利普,

我开始调试你的代码,并对你想要返回的内容感到有些困惑。在您的代码中,您在创建搜索过滤器时使用OR运算符,但在您的文本中,您将所需的输出描述为

  

只有带有附件的未读电子邮件应退回,他们不应将FATS或Sandbox:作为主题分配。

我接受了您尝试过滤的参数,并提出了以下过滤器,该过滤器将所有过滤器与可在我的机器上运行的逻辑AND结合使用:

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

FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, new ItemView(100));

有几点需要注意:

  • 我没有使用通用列表。我创建了一个带有AND逻辑运算符的SearchFilterCollection,然后将过滤器添加到该集合中。
  • 我的测试是使用我的本地邮箱,因此我没有绑定到其他邮箱并获取收件箱的文件夹ID。但是,这不应该对您的代码产生影响。
  • 我使用EmailMessageSchema.Subject而不是ItemSchema.Subject。我在测试中也使用了自己的字符串值,但是我将字符串值放在了示例中。

当我运行测试时,如果首先使用附件过滤未读消息。然后,我确认在添加主题过滤器后,返回的结果会进一步过滤。

我希望这会有所帮助。