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.
Chilkat Delphi DLL Downloads
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://www.googleapis.com/auth/gmail.imap_admin');
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;
// With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
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);