I'm reading emails from exchange server using Exchange web service(EWS).
I want to read emails from exchange server having DateTimeSent
greater than datetime.now
.
I haven't used searchfilter
and viewbase which are parameters of FindItems
, how to use them?
//Email exchange starts here
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
{
Credentials = new WebCredentials(credentials.SmtpUser, credentials.SmtpPassword)
};
//to add logic for itemview
service.AutodiscoverUrl(credentials.SmtpFromAddress, RedirectionUrlValidationCallback);
//var inbox = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100));
var inbox = service.FindItems(WellKnownFolderName.Inbox,); //what to add here?
foreach (EmailMessage item in inbox.Items.Where(x => Convert.ToDateTime(x.DateTimeSent) > dateTime.Now))
{
}
I have no idea how DateTimeSent
can be greater than DateTime.Now
and why would you need it, but the following is a sample how can you perform the search:
string email = "<user>@<host>";
string user = "<user>";
string password = "<password>";
string serviceUrl = "https://<url>/ews/exchange.asmx";
Mailbox mailbox = new Mailbox(email);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials(user, password);
service.Url = new Uri(serviceUrl);
FolderId inbox = new FolderId(WellKnownFolderName.Inbox, mailbox);
SearchFilter searchFilter = new SearchFilter.IsGreaterThan(ItemSchema.DateTimeSent, DateTime.Now);
ItemView view = new ItemView(10); // take 10 items
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
FindItemsResults<Item> result = service.FindItems(inbox, searchFilter, view);