Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.JsonArray,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  rows: TJsonArray;
  row: TJsonObject;
  i: Integer;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  success := False;
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
  http.AuthToken := 'SENDGRID_API_KEY';
  resp := THttpResponse.Create;

  //  SetUrlVar encodes the raw address as a URL component, including the plus sign.
  http.SetUrlVar('address','avery+orders@example.com');

  success := http.HttpNoBody('GET','https://api.sendgrid.com/v3/suppression/bounces/{$address}',resp);
  if (success <> True) then
    begin
      if (resp.StatusCode = 0) then
        begin
          WriteLn(http.LastErrorText);
        end
      else
        begin
          WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
        end;
      Exit;
    end;
  if (resp.StatusCode <> 200) then
    begin
      WriteLn('HTTP ' + resp.StatusCode + ': ' + resp.BodyStr);
      Exit;
    end;

  rows := TJsonArray.Create;
  success := rows.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Unexpected response: expected JSON.');
      Exit;
    end;
  if (rows.Size = 0) then
    begin
      WriteLn('No bounce record for this address.');
      Exit;
    end;
  row := TJsonObject.Create;
  i := 0;
  while (i < rows.Size) do
    begin
      rows.ObjectAt2(i,row);
      WriteLn(row.StringOf('email') + ': ' + row.StringOf('status') + ' ' + row.StringOf('reason'));
      i := i + 1;
    end;



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