7

I'm attempting to get a count of ALL of the emails in a user's mailbox using the Gmail API. That means running something like

do{
    page = request.execute();
    if (page.getMessages() != null){
        totalSize += page.getMessages().size();
    }
    request.setPageToken(page.getNextPageToken());
}while (request.getPageToken() != null && request.getPageToken().length() > 0);

The issue is that for large mailboxes, this can be a long process, taking many API calls. For instance, one mailbox with 31,000 emails takes well over a minute to run, and takes 300+ iterations to do so.

Is there a better way?

I've tried using the maxResults parameter, but it defaults to 100 and ignores any values >100. Batch processing wouldn't work, because there's no way to know how many requests you have to make until you've iterated through every page already. And the resultSizeEstimate property in the List response is absurdly inaccurate.

2
  • 1
    since current version of Gmail API is version 1, hence they are not providing much flexible functionality.May be in later versions they include this. Commented Sep 5, 2014 at 7:04
  • 1
    see my comment. both counts on labels and entire mailbox are provided now.
    – Eric D
    Commented Oct 16, 2014 at 16:22

1 Answer 1

9

The Gmail API now provides total and unread counts for messages and threads on any Label using the Labels.Get() method. See: https://developers.google.com/gmail/api/release-notes and https://developers.google.com/gmail/api/v1/reference/users/labels

If you want to get message and thread counts for the entire mailbox (All mail) then use the new gmail.users.getProfile() method. You can try it out at the API explorer: https://developers.google.com/gmail/api/v1/reference/users/getProfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.