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.
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://mail.google.com/");
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;
}
// List the labels visible through ordinary full-mail IMAP access.
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();
}
}