Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.1.0+

Gmail IMAP: Workspace Domain-Wide Delegation

See more GMail SMTP/IMAP/POP Examples

Use a JSON service-account key to impersonate a Workspace user for IMAP with https://www.googleapis.com/auth/gmail.imap_admin. A Workspace administrator must authorize the service account's numeric OAuth client ID for this exact scope in domain-wide delegation. The impersonated user must have an active Gmail mailbox.

This IMAP-only scope reveals labels hidden by Show in IMAP and messages excluded by the user's folder-size limit. It does not authorize SMTP or POP and is not available for personal Gmail accounts or ordinary user-consent flows. Workspace administrative access policies still apply.

Protect the private key and limit who can execute the delegated application. The signed JWT exchange produces a short-lived token without a refresh token; obtain another when necessary and reconnect. Keep the computer clock accurate. This example lists folders and examines INBOX read-only without printing credentials.

Google documentation

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
mailbox: WideString;
key: TChilkatJsonObject;
auth: TChilkatAuthGoogle;
connection: TChilkatSocket;
imap: TChilkatImap;
folders: TMailboxes;
i: Integer;

begin
success := 0;

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

mailbox := 'user@example.com';
key := TChilkatJsonObject.Create(Self);
success := key.LoadFile('gmail-service-account.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load the private service-account key.');
    Exit;
  end;
if ((key.StringOfEquals('type','service_account',1) <> 1) or (key.JsonTypeOf('private_key') <> 1)) then
  begin
    Memo1.Lines.Add('Expected a Google service-account JSON key.');
    Exit;
  end;
auth := TChilkatAuthGoogle.Create(Self);
auth.JsonKey := key.Emit();
auth.SubEmailAddress := mailbox;
auth.Scope := 'https://www.googleapis.com/auth/gmail.imap_admin';
auth.ExpireNumSeconds := 3600;
connection := TChilkatSocket.Create(Self);
connection.RequireSslCertVerify := 1;
success := connection.Connect('www.googleapis.com',443,1,10000);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not establish the Google token connection.');
    Exit;
  end;
success := auth.ObtainAccessToken(connection.ControlInterface);
connection.Close(1000);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Check the key, domain-wide delegation client ID/scope, user, and computer clock.');
    Exit;
  end;
imap := TChilkatImap.Create(Self);
imap.Ssl := 1;
imap.Port := 993;
imap.RequireSslCertVerify := 1;
imap.AuthMethod := 'XOAUTH2';
success := imap.Connect('imap.gmail.com');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not establish the Gmail IMAP TLS connection.');
    Exit;
  end;
success := imap.Login(mailbox,auth.AccessToken);
if (success <> 1) then
  begin
    imap.Disconnect();
    Memo1.Lines.Add('IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.');
    Exit;
  end;
folders := TMailboxes.Create(Self);
success := imap.MbxList(0,'','*',folders.ControlInterface);
if (success <> 1) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('Could not list Gmail folders.');
    Exit;
  end;
//  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
i := 0;
while (i < folders.Count) do
  begin
    Memo1.Lines.Add(folders.GetName(i));
    i := i + 1;
  end;

success := imap.ExamineMailbox('INBOX');
if (success <> 1) then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('Could not examine the impersonated user''s INBOX.');
    Exit;
  end;
Memo1.Lines.Add('INBOX messages: ' + IntToStr(imap.NumMessages));
imap.Logout();
imap.Disconnect();