searchFilter不能正常工作与EWS FindItems方法调用 [英] searchFilter not working properly with EWS FindItems method call
问题描述
我使用SearchFilter集合来限制使用EWS的Exchange 2010邮箱的请求返回的电子邮件。
I Am using a SearchFilter collection to restrict what emails are returned by a request to an Exchange 2010 mailbox using EWS.
连接到服务没有问题,并打开邮箱。
I have no problem connecting to the service, and opening the mailbox.
问题是我的searchFilter被忽略,所有的电子邮件都被请求发送到EWS。
The problem is that my searchFilter is being ignored, and all the emails are being returned by the request to 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:作业作为主题。
So, according to my understanding of the searchFilter, only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.
但这不是工作中,EWS的请求只是返回所有的电子邮件。
But that's not working, the request to EWS just returnes all the emails.
我在做什么错吗?
推荐答案
Philip,
我开始调试你的代码,对你想要返回的东西有点困惑。在您的代码中,您在创建搜索过滤器时使用OR运算符,但在文本中,您将所需的输出描述为
I started debugging your code and got a little confused as to what you are trying to get returned. In your code you are using an OR operator when you create the search filter, but in your text you describe the required output as
只读未读应该返回具有附件的电子邮件和,它们不应该具有FATS或Sandbox:作为主题分配。
only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.
我拿出了您尝试过滤的参数,并提出了以下过滤器,它将所有的过滤器与逻辑AND组合在一起,在我的机器上工作:
I took the parameters that you were trying to filter on and came up with the following filter that combines all the filters with a logical AND that works on my machine:
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
。我也在我的测试中使用了我自己的字符串值,但我把你的字符串值放在示例中。
- I'm not using a generic list. I created a
SearchFilterCollection
with an AND logical operator and then added the filters to that collection. - My testing was with my local mailbox so I didn't bind to a different mailbox and get the folder id for the inbox. That shouldn't make a difference in your code though.
- I used the
EmailMessageSchema.Subject
rather thanItemSchema.Subject
. I also used my own string values in my testing but I placed your string values in the example.
当我运行我的测试,如果首先在带有附件的未读邮件上进行过滤。当我添加主题过滤器后,我再验证返回的结果进一步过滤。
When I ran my tests, if first filtered on unread messages with an attachment. I then verified that the returned results filtered further when I added the subject filter.
希望这有帮助。
这篇关于searchFilter不能正常工作与EWS FindItems方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!