searchFilter在使用EWS FindItems方法调用时无法正常工作

4

我正在使用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 只应返回未读带有附件的电子邮件,而且主题不应为 FATSSandbox: Assignment

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

请问我做错了什么?

1个回答

9

菲利普,

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

只返回未读带附件的邮件并且它们的主题不应该是FATS或Sandbox: Assignment。

我以你尝试进行筛选的参数为基础,得出了以下可在我的机器上运行的逻辑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。在我的测试中,我还使用了自己的字符串值,但将您的字符串值放入例子中。

当我运行我的测试时,首先对带有附件的未读邮件进行了过滤。然后,当我添加主题过滤器时,验证了返回结果进一步过滤。

希望这能帮助到您。


1
菲利普,鲍勃说的话。 - Michael Mainer
+1 感谢 Bob,非常有帮助和信息量大,我以为我必须使用 AQS...现在不用了! - Our Man in Bananas
@Bob Bunn - 微软公司:有没有办法获取唯一的项目ID,以便我稍后可以在我的程序中将其标记为已读? - Our Man in Bananas
@MichaelMainer-Microsoft 有没有办法获取唯一的项目ID,以便我可以在程序中将其标记为已读? - Our Man in Bananas
3
你可以查看结果中每个项目的 Id 属性。如果你知道确切的项目,可以使用类似这样的代码:results.items[0].Id,它会给你所需的 GUID。之后,你可以将其绑定到 ItemId 上,并设置 isRead 属性。 - Bob Bunn
@BobBunn-Microsoft:嗨Bob,感谢你的所有帮助,对于SO:Exchange Web Services - Convert emailitem attachment from Base64 string to Byte gives error,你有什么想法吗? - Our Man in Bananas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接