title: Gmail-Api - C#: messages.list: count number of emails for a given day.
body: <p>Using the C# <a href="https://developers.google.com/gmail/api/" rel="nofollow">Gmail - Api</a>, I try to count to number of emails for a given day. </p>
  
  <p>I use the following code:</p>
  
  <pre>
  public static int CountMessages(GmailService service, String userId, String query)
   {
              int numberOfMessages = 0;
  
              UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
              request.Q = query;
  
              do
              {
                  try
                  {
                      ListMessagesResponse response = request.Execute();
                      if (response.Messages != null)
                      {
                          numberOfMessages += response.Messages.Count;
                      }
  
                      Console.WriteLine(request.PageToken);
  
                      request.PageToken = response.NextPageToken;
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("An error occurred: " + e.Message);
                  }
              } while (!String.IsNullOrEmpty(request.PageToken));
  
              return numberOfMessages;
  }
  </pre>
  
  <p>my query is something like this: <pre>after:2014/05/25 before:2014/05/26 in:anywhere (something)</pre></p>
  
  <p>When a execute the query in gmail, I don't have the same number of results as my code (more or less results but never the same amount).</p>
  
  <p>Is there anything wrong in my code ?</p>
  
  <p>references:</p>
  
  <p>
  <li><a href="https://developers.google.com/gmail/api/overview#api_overview" rel="nofollow">Gmail api</a></li>
  <li><a href="https://developers.google.com/gmail/api/guides/filtering" rel="nofollow">Searching for Messages</a></li>
  <li><a href="https://developers.google.com/gmail/api/v1/reference/users/messages/list" rel="nofollow">Users.messages: list</a></li></p>