Sample code for 30+ languages & platforms
Unicode C++ 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 Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkAuthGoogleW.h>
#include <CkSocketW.h>
#include <CkImapW.h>
#include <CkMailboxesW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    const wchar_t *mailbox = L"user@example.com";
    CkJsonObjectW key;
    success = key.LoadFile(L"gmail-service-account.json");
    if (success != true) {
        wprintf(L"Could not load the private service-account key.\n");
        return;
    }

    if ((key.StringOfEquals(L"type",L"service_account",true) != true) || (key.JsonTypeOf(L"private_key") != 1)) {
        wprintf(L"Expected a Google service-account JSON key.\n");
        return;
    }

    CkAuthGoogleW auth;
    auth.put_JsonKey(key.emit());
    auth.put_SubEmailAddress(mailbox);
    auth.put_Scope(L"https://www.googleapis.com/auth/gmail.imap_admin");
    auth.put_ExpireNumSeconds(3600);
    CkSocketW connection;
    connection.put_RequireSslCertVerify(true);
    success = connection.Connect(L"www.googleapis.com",443,true,10000);
    if (success != true) {
        wprintf(L"Could not establish the Google token connection.\n");
        return;
    }

    success = auth.ObtainAccessToken(connection);
    connection.Close(1000);
    if (success != true) {
        wprintf(L"Check the key, domain-wide delegation client ID/scope, user, and computer clock.\n");
        return;
    }

    CkImapW imap;
    imap.put_Ssl(true);
    imap.put_Port(993);
    imap.put_RequireSslCertVerify(true);
    imap.put_AuthMethod(L"XOAUTH2");
    success = imap.Connect(L"imap.gmail.com");
    if (success != true) {
        wprintf(L"Could not establish the Gmail IMAP TLS connection.\n");
        return;
    }

    success = imap.Login(mailbox,auth.accessToken());
    if (success != true) {
        imap.Disconnect();
        wprintf(L"IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n");
        return;
    }

    CkMailboxesW folders;
    success = imap.MbxList(false,L"",L"*",folders);
    if (success != true) {
        imap.Logout();
        imap.Disconnect();
        wprintf(L"Could not list Gmail folders.\n");
        return;
    }

    //  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
    int i = 0;
    while ((i < folders.get_Count())) {
        wprintf(L"%s\n",folders.getName(i));
        i = i + 1;
    }

    success = imap.ExamineMailbox(L"INBOX");
    if (success != true) {
        imap.Logout();
        imap.Disconnect();
        wprintf(L"Could not examine the impersonated user's INBOX.\n");
        return;
    }

    wprintf(L"INBOX messages: %d\n",imap.get_NumMessages());
    imap.Logout();
    imap.Disconnect();
    }