title: Get ItemId of next email given ItemId of current email, using Exchange Web Services (EWS) Managed API
body: <p>I need to write a method in C# that uses the Exchange Web Services (EWS) Managed API to read emails from a mailbox and given the ItemId/UniqueId of the current email, returns the ItemId/UniqueId of the next email in the inbox after the current email.</p>
  
  <p>Furthermore, for various reasons I require the method to be a static stateless method, that is, it cannot rely on any member/global variables that persist between calls to the method. Thus I can't simply store a reference to an instance of a FindItemsResults object and move to the next Item each time the method is called.</p>
  
  <p>I have tried implementing the method using the following code (simplified example only, no error checking):</p>
  
  <pre><code>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&lt;Item&gt; findResults = service.FindItems(WellKnownFolderName.Inbox, sfNextEmail, itemView);
      // return unique ID of next email
      return findResults.Items[0].Id.UniqueId;
  }
  </code></pre>
  
  <p>However when I run this method it throws the following exception from the service.FindItems line:</p>
  
  <p>System.ArgumentException: "Validation failed. Parameter name: searchFilter"<br>
  Inner exception - Microsoft.Exchange.WebServices.Data.ServiceValidationException: "Values of type 'ItemId' cannot be as comparison values in search filters."  </p>
  
  <p>One option is to use FindItems to find all the emails in the Inbox and iterate through them checking the ItemId until I find the current email, then move to the next one and return its unique ID. But I think this is likely to be slow and inefficent. I am hoping there is a better way to achieve this.</p>
  
  <p>Any help or suggestions would be greatly appreciated. I haven't been able to find any solutions searching the web.</p>