Sample code for 30+ languages & platforms
Unicode C++ 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 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://mail.google.com/");
    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;
    }

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