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

C
#include <C_CkImap.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *mailbox;
    HCkImap imap;

    success = FALSE;

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

    mailbox = "user@example.com";
    imap = CkImap_Create();
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    CkImap_putRequireSslCertVerify(imap,TRUE);
    success = CkImap_Connect(imap,"imap.gmail.com");
    if (success != TRUE) {
        printf("Could not establish the Gmail IMAP TLS connection.\n");
        CkImap_Dispose(imap);
        return;
    }

    success = CkImap_Login(imap,mailbox,"YOUR_16_CHARACTER_APP_PASSWORD");
    if (success != TRUE) {
        CkImap_Disconnect(imap);
        printf("IMAP authentication failed. Check the app password, mailbox, and account policy.\n");
        CkImap_Dispose(imap);
        return;
    }

    //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
    success = CkImap_ExamineMailbox(imap,"INBOX");
    if (success != TRUE) {
        CkImap_Logout(imap);
        CkImap_Disconnect(imap);
        printf("Could not examine INBOX.\n");
        CkImap_Dispose(imap);
        return;
    }

    printf("Messages in INBOX: %d\n",CkImap_getNumMessages(imap));
    success = CkImap_Logout(imap);
    CkImap_Disconnect(imap);
    if (success != TRUE) {
        printf("The read-only operation completed, but IMAP logout failed.\n");
    }



    CkImap_Dispose(imap);

    }