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

Gmail IMAP: Connect with an App Password

Authenticate to Gmail over implicit TLS on port 993, then open INBOX read-only and report its message count. No message is downloaded or changed. This replaces ordinary-password login and removes authentication-session logging.

Personal Gmail IMAP is always enabled. Workspace administrators can restrict IMAP access. For OAuth2, use Gmail IMAP token authentication.

Use an account-specific app password generated after enabling 2-Step Verification, loaded from protected configuration. A normal Google password will not work. App passwords may be unavailable under account or Workspace policy; OAuth2 is preferred. Changing the Google account password revokes app passwords. The obsolete less-secure-apps switch is not a setup option. Google app-password requirements.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

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.Imap;

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

procedure RunDemo;
var
  success: Boolean;
  mailbox: string;
  imap: TImap;

begin
  success := False;

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

  mailbox := 'user@example.com';
  imap := TImap.Create;
  imap.Ssl := True;
  imap.Port := 993;
  imap.RequireSslCertVerify := True;
  success := imap.Connect('imap.gmail.com');
  if (success <> True) then
    begin
      WriteLn('Could not establish the Gmail IMAP TLS connection.');
      Exit;
    end;
  success := imap.Login(mailbox,'YOUR_16_CHARACTER_APP_PASSWORD');
  if (success <> True) then
    begin
      imap.Disconnect();
      WriteLn('IMAP authentication failed. Check the app password, mailbox, and account policy.');
      Exit;
    end;
  //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
  success := imap.ExamineMailbox('INBOX');
  if (success <> True) then
    begin
      imap.Logout();
      imap.Disconnect();
      WriteLn('Could not examine INBOX.');
      Exit;
    end;
  WriteLn('Messages in INBOX: ' + imap.NumMessages);
  success := imap.Logout();
  imap.Disconnect();
  if (success <> True) then
    begin
      WriteLn('The read-only operation completed, but IMAP logout failed.');
    end;


  imap.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.