How to quickly get the number of messages in a user's mailbox?

I am trying to get an account of ALL emails in a user's mailbox using the Gmail API. It means 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 problem is that for large mailboxes this can be a long process, taking many API calls. For example, one mailbox with 31,000 letters takes more than a minute to work, and this requires more than 300 iterations.

Is there a better way?

I tried to use the maxResults parameter, but by default it is 100 and ignores any values> 100. Batch processing will not work, because there is no way to know how many queries you have to complete until you iterate through each page. And the property resultSizeEstimatein the List response is absurdly inaccurate.

+4

All Articles