如何在microsoft exchange web service (EWS)中使用搜索过滤器和viewbase
本文关键字:搜索 过滤器 viewbase EWS microsoft exchange web service | 更新日期: 2023-09-27 18:05:47
我正在使用exchange web service(EWS)从exchange服务器读取电子邮件。
我想阅读来自DateTimeSent
大于datetime.now
的exchange服务器的电子邮件。
我没有使用searchfilter
和viewbase,它们是FindItems
的参数,如何使用它们?
//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))
{
}
我不知道DateTimeSent
如何大于DateTime.Now
以及为什么需要它,但以下是一个如何执行搜索的示例:
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);