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的理解,只应返回带有附件的未读电子邮件,并且不应将 FATS或Sandbox: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));
有几点需要注意:
当我运行测试时,如果首先使用附件过滤未读消息。 然后,我确认在添加主题filter时,返回的结果会进一步过滤。
我希望这有帮助。
上述就是C#学习教程:searchFilter与EWS FindItems方法调用无法正常工作分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1041963.html