Exchange Web服务:查找发送给收件人的电子邮件

时间:2013-08-08 13:03:35

标签: exchange-server exchangewebservices

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

我可以通过电子邮件地址过滤邮箱中的电子邮件,如下所示:

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");

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

3 个答案:

答案 0 :(得分: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进行复杂的搜索

答案 1 :(得分:5)

我没有找到使用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。

查询语法的一些其他资源:

答案 2 :(得分:1)

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

我是这样做的:

 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
        }

    }`