Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Get a List of Message IDs in GMail User's Mailbox

See more GMail REST API Examples

Demonstrates how to get a list of message IDs in a GMail mailbox. The "userId" can be either the user's email address or the special value "me" to indicate the authenticated user.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  jsonResponse: TJsonObject;
  id: string;
  threadId: string;
  resultSizeEstimate: Integer;
  i: Integer;
  count_i: Integer;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  http.AuthToken := 'ACCESS_TOKEN';

  http.Accept := 'application/json';

  resp := THttpResponse.Create;
  success := http.HttpNoBody('GET','https://www.googleapis.com/gmail/v1/users/userId/messages',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response Status Code: ' + resp.StatusCode);

  jsonResponse := TJsonObject.Create;
  jsonResponse.Load(resp.BodyStr);
  jsonResponse.EmitCompact := False;
  WriteLn(jsonResponse.Emit());

  if (resp.StatusCode <> 200) then
    begin
      WriteLn('Failed.');
      Exit;
    end;

  //  {
  //    "messages": [
  //      users.messages Resource
  //    ],
  //    "nextPageToken": string,
  //    "resultSizeEstimate": unsigned integer
  //  }

  resultSizeEstimate := jsonResponse.IntOf('resultSizeEstimate');
  i := 0;
  count_i := jsonResponse.SizeOfArray('messages');
  while i < count_i do
    begin
      jsonResponse.I := i;
      id := jsonResponse.StringOf('messages[i].id');
      threadId := jsonResponse.StringOf('messages[i].threadId');
      i := i + 1;
    end;



  http.Free;
  resp.Free;
  jsonResponse.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.