2017-02-08 5 views
-1

今日EWSを使用して送信された電子メールを検索しています。私はこれらの日付フィルタリングされたメールを、件名と本文のキーワードを検索するために渡したいので、今日送られてきたメールを取得して、件名にキーワードを持たせることができます。コードで私を助けてください。ここで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; 
    } 

注:これは私の最初の質問です。だから、質問に答えるために必要な情報を自由に聞いてください。

+0

を試してみては、特定の問題や動作していない何かがあるのか​​、単にそれを行う方法を知りませんか? –

+0

@SimonPrice **私はそれをする方法を知らない。 subjectFilterとbodyFilterは、受信トレイからすべてのメールをフィルタリングしています。** –

+0

これは実際にこのセクションではスタックされていません。別のスタックサイトでそれを尋ねたいかもしれません。しかし、ここであなたのためにこれを実行してください)しかし....私に接続するためのEWSがなければ、私はあなたを完全に手助けするのに苦労するでしょう。しかし、2つの質問、1あなたは接続したいメールサービスに接続することができますか? 2.最初の質問にyesと答えたら、あなたの 'searchResult'から何かが戻ってくるのですか、何かエラーが出ますか? –

答えて

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

    } 
} 
+0

私は電子メールの 'Demo'キーワードを検索したいと思います。 –

+0

私は答えを編集しました。あなたは今それを試すことができます –

+0

@SaswatRathはあなたのためにこの仕事をしましたか? –

関連する問題