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

Move a GMail Message to Trash

See more GMail REST API Examples

Moves a specific GMail email message to trash.

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.HttpResponse,
  Chilkat.Http;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  id: string;
  userId: string;
  url: string;
  resp: THttpResponse;

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 := 'GMAIL-ACCESS-TOKEN';

  //  The id of the GMail message to move to Trash.
  id := '16678c485e7f0a0c';
  userId := 'me';

  http.SetUrlVar('userId','me');
  http.SetUrlVar('id',id);

  //  Move to trash by POSTing w/ an empty request body.
  url := 'https://www.googleapis.com/gmail/v1/users/{$userId}/messages/{$id}/trash';
  resp := THttpResponse.Create;
  success := http.HttpStr('POST',url,'','','',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('status = ' + resp.StatusCode);

  //  A 200 response status indicate success.
  if (resp.StatusCode <> 200) then
    begin
      WriteLn(resp.BodyStr);
      WriteLn('Failed.');
      Exit;
    end;

  //  A successful repsonse contains JSON that looks like this:

  //  {
  //   "id": "16678c485e7f0a0c",
  //   "threadId": "16678c485e7f0a0c",
  //   "labelIds": [
  //    "TRASH",
  //    "CATEGORY_SOCIAL"
  //   ]
  //  }

  WriteLn('response body:');
  WriteLn(resp.BodyStr);

  WriteLn('Message moved to trash!');


  http.Free;
  resp.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.