0
votes

Using the C# Gmail - Api, I try to count to number of emails for a given day.

I use the following code:

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;
}

my query is something like this:

after:2014/05/25 before:2014/05/26 in:anywhere (something)

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).

Is there anything wrong in my code ?

references:

  • Gmail api
  • Searching for Messages
  • Users.messages: list
  • 2

    2 Answers

    1
    votes

    For starters, your code is counting the number of messages, while Gmail displays the number of threads, and a thread may contain multiple messages.