10

我正在使用 Exchange Web 服务连接到邮箱并使用FindItemsSearchFilter查找符合特定条件的邮件。

我可以在邮箱过滤“发件人”电子邮件地址中收到电子邮件,如下所示:

var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
            {
                TraceEnabled = true,
                Credentials = new WebCredentials(username, password)
            };

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.From, "some@email.com");

service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50))

我可以DisplayTo像这样过滤属性:

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.DisplayTo, "display name");

但据我所知,仅搜索收件人的显示名称。我想搜索电子邮件地址或域名。

当我期望它时,这不会返回结果:

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.ToRecipients, "some@email.com");

是否可以找到收件人列表包含指定电子邮件地址的所有电子邮件?

4

3 回答 3

8

我没有找到使用 SearchFilter 根据收件人电子邮件地址查找电子邮件的方法。

可以使用带有查询字符串的不同的 ExchangeService.FindItems 重载。

查找地址位于“收件人”或“抄送”字段中的电子邮件

var contactEmailAddress = "some@email.com";

var querystring = string.Format("Participants:={0}", contactEmailAddress);

service.FindItems(WellKnownFolderName.Inbox, queryString, view);

查找地址位于“发件人”、“收件人”或“抄送”字段中的电子邮件

var contactEmailAddress = "some@email.com";

var querystring = string.Format("(From:={0} OR Participants:={0})", contactEmailAddress);

service.FindItems(WellKnownFolderName.Inbox, queryString, view);

我认为此功能需要 Exchange 2010。

有关查询语法的一些附加资源:

于 2013-09-04T15:43:13.973 回答
6

这可能是因为您没有访问正确的文件夹,即:已发送的项目。

代替

service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50))

经过

service.FindItems(WellKnownFolderName.SentItems, filter, new ItemView(50))

编辑:我误解了最初的问题。也许你应该看看下面的 MSDN 博客:http: //blogs.msdn.com/b/akashb/archive/2010/03/05/how-to-build-a-complex-search-using-searchfilter- and-searchfiltercollection-in-ews-managed-api-1-0.aspx 它解释了如何使用 EWS 进行复杂的搜索

于 2013-09-04T15:26:17.590 回答
1

请注意,您不能扩展FindItems方法或FindItem操作来检索其他属性和元素,例如ToRecipientsCcRecipientsBccRecipients。如果您需要检索这些值,请使用 FindItems 方法或 FindItem 操作来获取电子邮件的项目 ID,然后使用 Bind 方法或 GetItem 操作来检索所需的属性。参考:MSDN 电子邮件属性

我是这样做的:

 ItemView view = new ItemView(200);
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.DateTimeSent);

    List<SearchFilter> searchFilterCollection = new List<SearchFilter>();   
    searchFilterCollection.Add(new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeSent, LastServiceRun)); //Fetching recently sent emails 
    //In case you want to have more than one filter    
    SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);

    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, searchFilter, view);

    foreach (Microsoft.Exchange.WebServices.Data.EmailMessage item in results)
    {
        PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.ToRecipients);

        EmailMessage sentEmail = (EmailMessage)Item.Bind(service, item.Id, propSet);

        if (sentEmail.ToRecipients.Any(sEmail => sEmail.Address == mySearchEmailAddress))
        {
            //An email found where an address is in the To field - Your logic comes here
        }

    }`
于 2017-06-29T09:18:04.417 回答