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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sMailbox
    Handle hoImap
    Integer iTemp1

    Move False To iSuccess

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

    Move "user@example.com" To sMailbox
    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Set ComRequireSslCertVerify Of hoImap To True
    Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not establish the Gmail IMAP TLS connection."
        Procedure_Return
    End

    Get ComLogin Of hoImap sMailbox "YOUR_16_CHARACTER_APP_PASSWORD" To iSuccess
    If (iSuccess <> True) Begin
        Get ComDisconnect Of hoImap To iSuccess
        Showln "IMAP authentication failed. Check the app password, mailbox, and account policy."
        Procedure_Return
    End

    //  EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
    Get ComExamineMailbox Of hoImap "INBOX" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not examine INBOX."
        Procedure_Return
    End

    Get ComNumMessages Of hoImap To iTemp1
    Showln "Messages in INBOX: " iTemp1
    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess <> True) Begin
        Showln "The read-only operation completed, but IMAP logout failed."
    End



End_Procedure