Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

List Gmail message IDs across all pages

See more GMail REST API Examples

List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
total: Integer;
baseUrl: HCkStringBuilder;
url: HCkStringBuilder;
pageToken: HCkStringBuilder;
more: Boolean;
count: Integer;
i: Integer;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

token := CkJsonObject_Create();
success := CkJsonObject_LoadFile(token,'gmail-token.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read gmail-token.json from the private working directory.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
total := 0;
baseUrl := CkStringBuilder_Create();
CkStringBuilder_Append(baseUrl,'https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100');
url := CkStringBuilder_Create();
pageToken := CkStringBuilder_Create();
more := True;
while (more = True) do
  begin
    CkStringBuilder_SetString(url,CkStringBuilder__getAsString(baseUrl));
    if (CkStringBuilder_getLength(pageToken) > 0) then
      begin
        CkStringBuilder_Append(url,'&pageToken=');
        CkStringBuilder_Append(url,CkHttp__urlEncode(http,CkStringBuilder__getAsString(pageToken)));
      end;
    success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
    if (CkHttpResponse_getStatusCode(resp) = 0) then
      begin
        Memo1.Lines.Add('Gmail did not return an HTTP response.');
        Exit;
      end;
    if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
      begin
        Memo1.Lines.Add('Gmail HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
        success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
        if (success = True) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.message'));
          end;
        Exit;
      end;
    success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
    if (success <> True) then
      begin
        Memo1.Lines.Add('Expected a Gmail JSON response.');
        Exit;
      end;
    //  Gmail can omit messages when the search or mailbox is empty.
    count := CkJsonObject_SizeOfArray(reply,'messages');
    i := 0;
    while (i < count) do
      begin
        CkJsonObject_putI(reply,i);
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'messages[i].id') + '  thread ' + CkJsonObject__stringOf(reply,'messages[i].threadId'));
        total := total + 1;
        i := i + 1;
      end;

    more := False;
    if (CkJsonObject_JsonTypeOf(reply,'nextPageToken') = 1) then
      begin
        CkStringBuilder_SetString(pageToken,CkJsonObject__stringOf(reply,'nextPageToken'));
        more := CkStringBuilder_getLength(pageToken) > 0;
      end;
  end;

Memo1.Lines.Add('Messages listed: ' + IntToStr(total));

CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkStringBuilder_Dispose(baseUrl);
CkStringBuilder_Dispose(url);
CkStringBuilder_Dispose(pageToken);