如何在一次更新中将通过 EWS 获取的所有电子邮件标记为已读?
How can I mark as read all emails fetched through EWS in a single update?
我遵循 MSDN 上的 EWS Managed API 示例 find all unread email in my Exchange mailbox account。
我后来遍历了每个找到的项目,以便将它们放入我需要的列表中return,同时获取每条消息的正文并将每个更新为 IsRead=true
,如下所示:
Folder.Bind(Service, WellKnownFolderName.Inbox);
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
//ItemView limits the results to numOfMails2Fetch items
FindItemsResults<Item> foundItems = Service.FindItems(WellKnownFolderName.Inbox, sf,
new ItemView(numOfMails2Fetch));
if (foundItems.TotalCount > 0)
{
List<EmailMessage> emailsList = new List<EmailMessage>(foundItems.TotalCount);
foundItems.Items.ToList().ForEach(item =>
{
var iEM = item as EmailMessage;
emailsList.Add(iEM);
// update properties
iEM.IsRead = true;
iEM.Update(ConflictResolutionMode.AutoResolve);
});
// fetches and assign the bodies of each email
Service.LoadPropertiesForItems(emailsList,PropertySet.FirstClassProperties);
return emailsList;
} else return null;
是否可以在单个请求中将所有找到的项目更新为 IsRead=true
? IE。无需一一更新 = 更好的性能和连贯的逻辑。
是的,你可以。 ExchangeService.UpdateItems
是您要在此处使用的方法。有关详细信息,请参阅 How to: Process email messages in batches by using EWS in Exchange。
我遵循 MSDN 上的 EWS Managed API 示例 find all unread email in my Exchange mailbox account。
我后来遍历了每个找到的项目,以便将它们放入我需要的列表中return,同时获取每条消息的正文并将每个更新为 IsRead=true
,如下所示:
Folder.Bind(Service, WellKnownFolderName.Inbox);
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
//ItemView limits the results to numOfMails2Fetch items
FindItemsResults<Item> foundItems = Service.FindItems(WellKnownFolderName.Inbox, sf,
new ItemView(numOfMails2Fetch));
if (foundItems.TotalCount > 0)
{
List<EmailMessage> emailsList = new List<EmailMessage>(foundItems.TotalCount);
foundItems.Items.ToList().ForEach(item =>
{
var iEM = item as EmailMessage;
emailsList.Add(iEM);
// update properties
iEM.IsRead = true;
iEM.Update(ConflictResolutionMode.AutoResolve);
});
// fetches and assign the bodies of each email
Service.LoadPropertiesForItems(emailsList,PropertySet.FirstClassProperties);
return emailsList;
} else return null;
是否可以在单个请求中将所有找到的项目更新为 IsRead=true
? IE。无需一一更新 = 更好的性能和连贯的逻辑。
是的,你可以。 ExchangeService.UpdateItems
是您要在此处使用的方法。有关详细信息,请参阅 How to: Process email messages in batches by using EWS in Exchange。