Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

Gmail IMAP: Connect with an App Password

Authenticate to Gmail over implicit TLS on port 993, then open INBOX read-only and report its message count. No message is downloaded or changed. This replaces ordinary-password login and removes authentication-session logging.

Personal Gmail IMAP is always enabled. Workspace administrators can restrict IMAP access. For OAuth2, use Gmail IMAP token authentication.

Use an account-specific app password generated after enabling 2-Step Verification, loaded from protected configuration. A normal Google password will not work. App passwords may be unavailable under account or Workspace policy; OAuth2 is preferred. Changing the Google account password revokes app passwords. The obsolete less-secure-apps switch is not a setup option. Google app-password requirements.

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 <C_CkImapW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *mailbox;
    HCkImapW imap;

    success = FALSE;

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

    mailbox = L"user@example.com";
    imap = CkImapW_Create();
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);
    CkImapW_putRequireSslCertVerify(imap,TRUE);
    success = CkImapW_Connect(imap,L"imap.gmail.com");
    if (success != TRUE) {
        wprintf(L"Could not establish the Gmail IMAP TLS connection.\n");
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_Login(imap,mailbox,L"YOUR_16_CHARACTER_APP_PASSWORD");
    if (success != TRUE) {
        CkImapW_Disconnect(imap);
        wprintf(L"IMAP authentication failed. Check the app password, mailbox, and account policy.\n");
        CkImapW_Dispose(imap);
        return;
    }

    //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
    success = CkImapW_ExamineMailbox(imap,L"INBOX");
    if (success != TRUE) {
        CkImapW_Logout(imap);
        CkImapW_Disconnect(imap);
        wprintf(L"Could not examine INBOX.\n");
        CkImapW_Dispose(imap);
        return;
    }

    wprintf(L"Messages in INBOX: %d\n",CkImapW_getNumMessages(imap));
    success = CkImapW_Logout(imap);
    CkImapW_Disconnect(imap);
    if (success != TRUE) {
        wprintf(L"The read-only operation completed, but IMAP logout failed.\n");
    }



    CkImapW_Dispose(imap);

    }