EWS邮件搜索条件搜索特定日期的邮件和主题行中的关键字

时间:2017-02-08 12:31:08

标签: c# email exchangewebservices

我正在搜索今天使用EWS发送的电子邮件。我想通过这些日期过滤的邮件来搜索主题行和正文中的关键字,以便我可以获取今天发送的邮件并在主题中包含关键字。请帮我解释一下代码。

这是我的代码:

             public static FindItemsResults<Item> MailSearchCriteria(string condition, ExchangeService exchangeService)
             {
        FindItemsResults<Item> searchResult = null;
        string mailsearch = "Demo";
        try
        {


            DateTime date = DateTime.Today;

            //A local variable filter stores the search condition according to the date
            SearchFilter greaterThanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
            SearchFilter lessThanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, date.AddDays(1));
            SearchFilter dateFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterThanfilter, lessThanfilter);

            //All the mails for the given date is stored in a variable
            searchResult = exchangeService.FindItems(WellKnownFolderName.Inbox, dateFilter, new ItemView(500));

            //Count of all the mails with date filter is stored in an integer variable
            int mailno = searchResult.Count();

            //Runs the loop for the count to be greater than 0
            while (mailno >= 0)
            {
                foreach (Item item in searchResult.Items)
                {

                    string sender= ((EmailMessage)(item)).Sender.Name;

                    if (sender=="sender_name")
                    {
                        //Subject filter criteria
                        //A local variable subjectFilter stores the subject filter pattern passed from the database
                        SearchFilter.ContainsSubstring subjectFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, mailsearch, ContainmentMode.Substring, ComparisonMode.IgnoreCase);

                        //Mail body filter criteria
                        //A local variable bodytFilter stores the body filter pattern passed from the database
                        SearchFilter.ContainsSubstring bodyFilter = new SearchFilter.ContainsSubstring(ItemSchema.Body, mailsearch, ContainmentMode.Substring, ComparisonMode.IgnoreCase);
                        //Checks the search condition passed from the xml file
                        if (Equals(condition, "OR") && (subjectFilter.Value != string.Empty || bodyFilter.Value != string.Empty))
                        {
                            //Logical OR condition for pattern search in subject or body is stored in a local variable
                            SearchFilter.SearchFilterCollection orFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, subjectFilter, bodyFilter);

                            //The mails satisfying the search criteria are stored in a variable
                            searchResult = exchangeService.FindItems(WellKnownFolderName.Inbox, orFilter, new ItemView(10));
                        }
                        else
                        {
                            //Logical AND condition for pattern search in subject and body is stored in a local variable
                            SearchFilter.SearchFilterCollection andFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, subjectFilter, bodyFilter);

                            //The mails satisfying the search criteria are stored in a variable
                            searchResult = exchangeService.FindItems(WellKnownFolderName.Inbox, andFilter, new ItemView(10));
                        }

                    }
                    else
                    {
                        //filelog error call
                    }

                }
                //The count is decremented
                mailno--;
            }

            //call fileLog method for success
            //fileLog(1,"successfully searched");
        }

        catch (Exception errormsg)
        {
            //filelog method call
            //fileLog(0,errormsg.Tostring);
        }
        return searchResult;
    }

注意:这是我的第一个问题。因此,请随时询问您回答问题所需的任何信息。

1 个答案:

答案 0 :(得分:0)

试试这个

void Main()
{
DateTime date = DateTime.Today;
var exchangeService = new ExchangeService();

SearchFilter greaterThanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
SearchFilter lessThanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, date.AddDays(1));
SearchFilter dateFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterThanfilter, lessThanfilter);

//All the mails for the given date is stored in a variable
var searchResult = exchangeService.FindItems(WellKnownFolderName.Inbox, dateFilter, new ItemView(500));

//Count of all the mails with date filter is stored in an integer variable
int mailno = searchResult.Count();

foreach (Item item in searchResult.Items)
{
    string sender = ((EmailMessage)(item)).Sender.Name;

    if (sender == "Sender_name")
    {
        //Subject filter criteria
        //A local variable subjectFilter stores the subject filter pattern passed from the database
        //**I want to pass the 'item' into the subjectFilter and bodyFilter**
                    SearchFilter.ContainsSubstring subjectFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Demo", ContainmentMode.Substring, ComparisonMode.IgnoreCase);

        //Mail body filter criteria
        //A local variable bodytFilter stores the body filter pattern passed from the database
        SearchFilter.ContainsSubstring bodyFilter = new SearchFilter.ContainsSubstring(ItemSchema.Body, "Demo", ContainmentMode.Substring, ComparisonMode.IgnoreCase);

    }
}