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

Our*_*nas 3 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!
Run Code Online (Sandbox Code Playgroud)

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

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

我做错了什么?

Bob*_*unn 7

菲利普,

我开始调试你的代码,并对你想要返回的内容感到有些困惑.在您的代码中,您在创建搜索过滤器时使用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));
Run Code Online (Sandbox Code Playgroud)

有几点需要注意:

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

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

我希望这有帮助.

  • 您可以查看结果中每个项目的Id属性.如果你知道确切的项目,你可以使用这样的东西:`results.items [0] .Id`它会给你你正在寻找的GUID.稍后您可以绑定到该ItemId,您可以设置isRead属性. (2认同)