加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:00:52 所属栏目:百科 来源:网络整理
导读:我正在使用SearchFilter集合来限制使用EWS将请求返回到Exchange 2010邮箱的电子邮件. 我没有问题连接到服务,并打开邮箱. 问题是我的searchFilter被忽略,所有电子邮件都被请求返回到EWS. 这是我的代码: static void Main(string[] args){ServicePointManager
我正在使用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:DevEWSHelloWorldattachments";

// 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运算符,但在文本中,您将所需的输出描述为

only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.

我获取了您尝试过滤的参数,并提出了以下过滤器,该过滤器将所有过滤器与可在我的机器上运行的逻辑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.我在测试中也使用了自己的字符串值,但是我将字符串值放在示例中.

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

我希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读