Gmail IMAP: Authenticate and Open the Inbox Read-Only
See more GMail SMTP/IMAP/POP Examples
Connect to imap.gmail.com:993 over implicit TLS, authenticate with XOAUTH2, and examine INBOX without changing messages. ExamineMailbox opens the mailbox read-only; the count is for the selected mailbox, not the entire Gmail account. Logout and disconnect explicitly when finished.
For personal Gmail accounts, IMAP has been always enabled since January 2025; the old Enable IMAP switch is no longer needed. Workspace administrators can restrict IMAP access and allowed OAuth clients. Use the mailbox that authorized the token, not the application's client ID.
Gmail exposes labels as IMAP folders. One message can appear under several labels, so summing folder counts can double-count it. Gmail special-folder names can be localized; INBOX is the protocol-defined exception. Discover other folders through LIST special-use attributes instead of assuming English names. Long-lived connections may be closed when the access token expires; refresh and reconnect.
This example reads gmail-mail-token.json from a private working directory. Authorize the account first, then refresh the access token before it expires. The separate filename avoids confusing this full-mail grant with a narrower Gmail REST token. Store credentials in protected application storage; never distribute a user's token file with an application.
Gmail SMTP, IMAP, and POP3 use the https://mail.google.com/ OAuth scope. Narrower Gmail REST scopes such as gmail.send or gmail.readonly do not authorize these protocols. Public applications using this restricted scope must meet Google's verification requirements. See Gmail OAuth scopes and XOAUTH2.
Chilkat Lazarus Pascal Downloads
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,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
token: TJsonObject;
mailbox: string;
imap: TImap;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TJsonObject.Create;
success := token.LoadFile('gmail-mail-token.json');
if (success <> True) then
begin
WriteLn('Could not read gmail-mail-token.json from the private working directory.');
Exit;
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
begin
WriteLn('An OAuth access token is required. Authorize or refresh the account first.');
Exit;
end;
mailbox := 'user@example.com';
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
imap.RequireSslCertVerify := True;
imap.AuthMethod := 'XOAUTH2';
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,token.StringOf('access_token'));
if (success <> True) then
begin
imap.Disconnect();
WriteLn('IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace 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;
token.Free;
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.