gpt4 book ai didi

c# - searchFilter 无法与 EWS FindItems 方法调用一起正常工作

转载 作者:行者123 更新时间:2023-11-30 14:10:43 26 4
gpt4 key购买 nike

我正在使用 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 的理解,只应返回带附件的未读电子邮件,并且它们应该没有 FATS沙盒:分配 作为主语。

但这不起作用,对 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,然后将过滤器添加到该集合。
  • 我的测试是使用我的本地邮箱进行的,因此我没有绑定(bind)到其他邮箱并获取收件箱的文件夹 ID。不过,这不会对您的代码产生影响。
  • 我使用了 EmailMessageSchema.Subject 而不是 ItemSchema.Subject。我在测试中也使用了自己的字符串值,但我将您的字符串值放在示例中。

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

希望对您有所帮助。

关于c# - searchFilter 无法与 EWS FindItems 方法调用一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22559704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com