问题描述
我正在使用EWS托管API在我的c#项目和我们的Exchange 2010服务器之间进行通信.从现在起和三天前,我都使用此代码来获取收件箱中的所有邮件.
Im using EWS Managed API to communicate between my c# project and our Exchange 2010 server.I use this code to get all mails in the inbox from now and three days back.
var ews = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ews.Credentials = new NetworkCredential(usr, psw, dmn);
ews.AutodiscoverUrl(url);
PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
itempropertyset.RequestedBodyType = BodyType.Text;
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;
do
{
findResults = ews.FindItems(WellKnownFolderName.Inbox, view);
foreach (Item item in findResults.Items)
{
if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue;
item.Load(itempropertyset);
var message = EmailMessage.Bind(ews, item.Id,
new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
string to = message.ToRecipients[0].Address.ToLower();
string body = item.Body;
}
view.Offset += findResults.TotalCount;
} while (findResults.MoreAvailable);
现在是问题所在.我想改进此行if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue;
,因为当我使用此行时,api从收件箱中获取所有消息,并且如果它的年龄超过三天,则继续.我想在代码的前面指定此过滤器,因此api不必处理所有消息.
Now the problem. I want to improve this line if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue;
because when i use this, the api gets all the messages from inbox and just continue if its older then three days. I want specify this filter earlier in the code, so the api dont have to handle all messages.
推荐答案
如果我正确理解了该问题,则应该可以使用.您可以在此处查看所有可用的搜索过滤器: EWS搜索过滤器
If I understand the problem correctly, this should work. You can see all search filters available here: EWS Search Filters
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;
SearchFilter searchFilter =
new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-3));
findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
这篇关于按日期获取Exchange EWS托管API 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!