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

我使用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 searchFilterCollection = new List(); 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 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的理解,只应返回带有附件的未读电子邮件,并且不应 FATSSandbox:Assignment作为主题。

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

我做错了什么?

菲利普,

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

只应返回带有附件的未读电子邮件, 并且不应将FATS或Sandbox:Assignment作为主题。

我采用了您尝试过滤的参数,并提出了以下filter,它将所有filter与在我的机器上运行的逻辑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 results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, new ItemView(100)); 

有几点需要注意:

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

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

我希望这有帮助。