使用Exchange Web服务(EWS)托管API获取当前电子邮件的ItemId的下一封电子邮件的ItemId

0 .net managed exchangewebservices ews-managed-api

我需要在C#中编写一个方法,使用Exchange Web服务(EWS)托管API从邮箱中读取电子邮件并给出当前电子邮件的ItemId/UniqueId,在收件箱中返回下一封电子邮件的ItemId/UniqueId.当前的电子邮件.

此外,由于各种原因,我要求该方法是一个静态无状态方法,也就是说,它不能依赖于在方法调用之间持续存在的任何成员/全局变量.因此,我不能简单地存储对FindItemsResults对象的实例的引用,并在每次调用该方法时移动到下一个Item.

我尝试使用以下代码实现该方法(仅简化示例,无错误检查):

using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
...
...
public string GetNextEmailId(string currentItemId)
{
    // set up Exchange Web Service connection
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    service.AutodiscoverUrl("example.user@contoso.com");
    // create SearchFilter to find next email after current email
    ItemId itemId = new ItemId(currentItemId);
    SearchFilter sfNextEmail = new SearchFilter.IsGreaterThan(EmailMessageSchema.Id, itemId);
    // find next email in inbox
    ItemView itemView = new ItemView(1);
    itemView.OrderBy.Add(EmailMessageSchema.DateTimeReceived, SortDirection.Ascending);
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sfNextEmail, itemView);
    // return unique ID of next email
    return findResults.Items[0].Id.UniqueId;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此方法时,它会从service.FindItems行抛出以下异常:

System.ArgumentException:"验证失败.参数名称:searchFilter"
内部异常--Microsoft.Exchange.WebServices.Data.ServiceValidationException :"类型'ItemId'的值不能作为搜索过滤器中的比较值."

一种选择是使用FindItems查找收件箱中的所有电子邮件并迭代检查ItemId,直到找到当前的电子邮件,然后移动到下一个并返回其唯一ID.但我认为这可能是缓慢而无效的.我希望有更好的方法来实现这一目标.

任何帮助或建议将不胜感激.我无法找到任何搜索网络的解决方案.

Chr*_*ris 6

如果您知道当前电子邮件的项目ID,则可以绑定到该项目:

EmailMessage current = EmailMessage.Bind(service,id);

然后,您将收到当前收到的电子邮件日期 - 为所有超过该日期的日期创建搜索过滤器,并按照您已有的代码使用您的订单.