我正在使用 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(@"[email protected]");
// 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 的请求只会返回所有电子邮件。
请问我做错了什么?
最佳答案
菲利普,
我开始调试您的代码,但对您尝试返回的内容感到有些困惑。在您的代码中,您在创建搜索过滤器时使用 OR 运算符,但在您的文本中,您将所需的输出描述为
我采用了您尝试过滤的参数,并提出了以下过滤器,该过滤器将所有过滤器与适用于我的机器的逻辑 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));
需要注意的几点:
SearchFilterCollection
,然后将过滤器添加到该集合中。 EmailMessageSchema.Subject
而不是 ItemSchema.Subject
。我还在测试中使用了我自己的字符串值,但我在示例中放置了您的字符串值。 当我运行我的测试时,如果首先过滤带有附件的未读邮件。然后,当我添加主题过滤器时,我验证了返回的结果是否进一步过滤。
我希望这有帮助。
关于c# - searchFilter 与 EWS FindItems 方法调用无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22559704/