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 Java Downloads
import com.chilkatsoft.chilkat2.AuthGoogle;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Mailboxes;
import com.chilkatsoft.chilkat2.Socket;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
String mailbox = "user@example.com";
JsonObject key = new JsonObject();
try {
key.loadFile("gmail-service-account.json");
} catch (ChilkatException e) {
System.out.println("Could not load the private service-account key.");
return;
}
if ((!key.stringOfEquals("type", "service_account", true)) || (key.jsonTypeOf("private_key") != 1)) {
System.out.println("Expected a Google service-account JSON key.");
return;
}
AuthGoogle auth = new AuthGoogle();
auth.setJsonKey(key.emit());
auth.setSubEmailAddress(mailbox);
auth.setScope("https://www.googleapis.com/auth/gmail.imap_admin");
auth.setExpireNumSeconds(3600);
Socket connection = new Socket();
connection.setRequireSslCertVerify(true);
try {
connection.connect("www.googleapis.com", 443, true, 10000);
} catch (ChilkatException e) {
System.out.println("Could not establish the Google token connection.");
return;
}
try {
auth.obtainAccessToken(connection);
success = true;
} catch (ChilkatException e) {
success = false;
}
connection.closeConnection(1000);
if (!success) {
System.out.println("Check the key, domain-wide delegation client ID/scope, user, and computer clock.");
return;
}
Imap imap = new Imap();
imap.setSsl(true);
imap.setPort(993);
imap.setRequireSslCertVerify(true);
imap.setAuthMethod("XOAUTH2");
try {
imap.connect("imap.gmail.com");
} catch (ChilkatException e) {
System.out.println("Could not establish the Gmail IMAP TLS connection.");
return;
}
try {
imap.login(mailbox, auth.getAccessToken());
} catch (ChilkatException e) {
imap.disconnect();
System.out.println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.");
return;
}
Mailboxes folders = new Mailboxes();
try {
imap.mbxList(false, "", "*", folders);
} catch (ChilkatException e) {
imap.logout();
imap.disconnect();
System.out.println("Could not list Gmail folders.");
return;
}
// With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
int i = 0;
while (i < folders.getCount()) {
System.out.println(folders.getName(i));
i = i + 1;
}
try {
imap.examineMailbox("INBOX");
} catch (ChilkatException e) {
imap.logout();
imap.disconnect();
System.out.println("Could not examine the impersonated user's INBOX.");
return;
}
System.out.println("INBOX messages: " + imap.getNumMessages());
imap.logout();
imap.disconnect();
}
}