2012-06-11 6 views
5

Tôi có thể tìm nạp email từ một hộp thư dựa trên một chủ đề. Tôi không chắc định dạng tìm nạp email dựa trên ngày nhận được là gì?Tìm nạp email cho một ngày cụ thể trong C# bằng cách sử dụng Dịch vụ Web Exchange

  string message = string.Empty; 
      Item item = Item.Bind(exService, messageID, PropertySet.FirstClassProperties); 

      if (item is EmailMessage) 
      { 
       EmailMessage em = (EmailMessage)item; 

       string strMsg = string.Empty; 
       //strMsg = strMsg + item.Id.ToString(); 
       //strMsg = strMsg + item.DateTimeReceived; 
       strMsg = strMsg + "*********************** New Fiscal Email received on " + item.DateTimeReceived +" ************************************" + Environment.NewLine; 

       if (em.Body.Text.Contains("BRANDON")) 
       { 
        strMsg = strMsg + em.Body.Text.ToString(); 
       } 
       strMsg = strMsg + "*********************** End of Email Body ************************************" + Environment.NewLine; 
       message = strMsg; 

      } 
+0

Mã bạn đã cung cấp ở trên không tìm kiếm theo chủ đề. Nó lấy thông điệp bằng 'EntryID'. – SliverNinja

Trả lời

3

Hãy xem SearchFilter examples. Bạn chỉ cần một điều kiện lọc trên ItemSchema.DateTimeReceived

+0

Cảm ơn SliverNinja! – acadia

+0

Tuy nhiên, tôi không thể có được định dạng nào tôi nên chuyển. khi tôi thử '5/12/2012', nó không truy xuất dữ liệu – acadia

+1

[Xem tại đây] (http://msdn.microsoft.com/en-us/library/ee693615.aspx) và tìm kiếm "Giới hạn phạm vi ngày " –

-2

Điều này sẽ hiệu quả.

if (em.DateTimeReceived.Equals(**Date you want to search**)) 
       { 
        strMsg = strMsg + em.Body.Text.ToString(); 
       } 
+1

Điều này sẽ không truy vấn EWS! Nó sẽ tải xuống tất cả các email anyway. Nó không trả lời những gì được hỏi trong câu hỏi. – Rahatur

16

Tôi nghĩ cách SilverNinja nói với bạn là đúng cách. Bạn nên tìm kiếm các mặt hàng như thế này:

DateTime searchdate = new DateTime (2012,7,6) //Year, month, day 
SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchdate); 
SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchdate.AddDays(1)); 
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter); 
Folder folder = Folder.Bind(this.m_Service, WellKnownFolderName.MsgFolderRoot); //Or the folder you want to search in 
FindItemsResults<Item> results = folder.FindItems(filter, new ItemView(1000)); 

"results.Items" sẽ trở lại với 1.000 mặt hàng đầu tiên được recivied vào ngày bạn đang tìm kiếm.