【问题标题】:searchFilter not working properly with EWS FindItems method callsearchFilter 无法与 EWS FindItems 方法调用一起正常工作
【发布时间】:2014-04-28 21:01:38
【问题描述】:

我正在使用 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 的理解,应该只返回带有附件的未读邮件,它们应该没有 FATSSandbox: Assignment em> 作为主语。

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

请问我做错了什么?

【问题讨论】:

    标签: c# email exchangewebservices searchfiltercollection


    【解决方案1】:

    菲利普,

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

    需要注意的几点:

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

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

    我希望这会有所帮助。

    【讨论】:

    • 菲利普,鲍勃说的。
    • +1 感谢 Bob,非常有帮助和信息,我以为我将不得不使用 AQS ......现在我不用了!
    • @Bob Bunn - Microsoft:有没有办法获得唯一的项目 ID,以便稍后在我的程序中将其标记为已读?
    • @MichaelMainer-Microsoft 有什么方法可以获取唯一的项目 ID,以便稍后在我的程序中将其标记为已读?
    • 您可以查看结果中每个项目的 Id 属性。如果您知道确切的项目,您可以使用如下内容:results.items[0].Id,它会为您提供您正在寻找的 GUID。稍后您可以绑定到该 ItemId 并设置 isRead 属性。
    猜你喜欢
    • 2022-07-05
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2020-08-19
    • 2013-02-06
    • 2019-02-02
    相关资源
    最近更新 更多