Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Gmail IMAP: Workspace Service Account with a JSON Key

Use a service-account JSON key with Google Workspace domain-wide delegation. A Workspace administrator must authorize the service account's numeric OAuth client ID for https://mail.google.com/. Set SubEmailAddress to the Workspace user whose active Gmail mailbox is being accessed, and use that same address for IMAP login.

A service account has no Gmail mailbox of its own; leaving the impersonated user empty does not grant access to a personal Gmail account. The signed JWT exchange yields a short-lived token without a refresh token. Obtain another when needed, protect the private key, and keep the computer clock accurate.

This example lists visible folders and examines INBOX read-only. For the different visibility rules of the IMAP-only administrative scope, see gmail.imap_admin delegation.

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

Reference documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mailbox: PWideChar;
key: HCkJsonObject;
auth: HCkAuthGoogle;
connection: HCkSocket;
imap: HCkImap;
folders: HCkMailboxes;
i: Integer;

begin
success := False;

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

mailbox := 'user@example.com';
key := CkJsonObject_Create();
success := CkJsonObject_LoadFile(key,'gmail-service-account.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load the private service-account key.');
    Exit;
  end;
if ((CkJsonObject_StringOfEquals(key,'type','service_account',True) <> True) or (CkJsonObject_JsonTypeOf(key,'private_key') <> 1)) then
  begin
    Memo1.Lines.Add('Expected a Google service-account JSON key.');
    Exit;
  end;
auth := CkAuthGoogle_Create();
CkAuthGoogle_putJsonKey(auth,CkJsonObject__emit(key));
CkAuthGoogle_putSubEmailAddress(auth,mailbox);
CkAuthGoogle_putScope(auth,'https://mail.google.com/');
CkAuthGoogle_putExpireNumSeconds(auth,3600);
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
success := CkSocket_Connect(connection,'www.googleapis.com',443,True,10000);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not establish the Google token connection.');
    Exit;
  end;
success := CkAuthGoogle_ObtainAccessToken(auth,connection);
CkSocket_Close(connection,1000);
if (success <> True) then
  begin
    Memo1.Lines.Add('Check the key, domain-wide delegation client ID/scope, user, and computer clock.');
    Exit;
  end;
imap := CkImap_Create();
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
CkImap_putRequireSslCertVerify(imap,True);
CkImap_putAuthMethod(imap,'XOAUTH2');
success := CkImap_Connect(imap,'imap.gmail.com');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not establish the Gmail IMAP TLS connection.');
    Exit;
  end;
success := CkImap_Login(imap,mailbox,CkAuthGoogle__accessToken(auth));
if (success <> True) then
  begin
    CkImap_Disconnect(imap);
    Memo1.Lines.Add('IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.');
    Exit;
  end;
folders := CkMailboxes_Create();
success := CkImap_MbxList(imap,False,'','*',folders);
if (success <> True) then
  begin
    CkImap_Logout(imap);
    CkImap_Disconnect(imap);
    Memo1.Lines.Add('Could not list Gmail folders.');
    Exit;
  end;
//  List the labels visible through ordinary full-mail IMAP access.
i := 0;
while (i < CkMailboxes_getCount(folders)) do
  begin
    Memo1.Lines.Add(CkMailboxes__getName(folders,i));
    i := i + 1;
  end;

success := CkImap_ExamineMailbox(imap,'INBOX');
if (success <> True) then
  begin
    CkImap_Logout(imap);
    CkImap_Disconnect(imap);
    Memo1.Lines.Add('Could not examine the impersonated user''s INBOX.');
    Exit;
  end;
Memo1.Lines.Add('INBOX messages: ' + IntToStr(CkImap_getNumMessages(imap)));
CkImap_Logout(imap);
CkImap_Disconnect(imap);

CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
CkMailboxes_Dispose(folders);