GMail IMAP NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)

You passed the correct login/password to login to imap.gmail.com, but you still get an “Invalid credentials” error.  What gives?

For example, you called Chilkat’s Imap.Login method, passing the correct login/password, and the imap.LastErrorText contains this:

authenticateLogin(110ms):
        loginImap(110ms):
          ConnectionType: SSL/TLS
          ImapCmdSent: aaab LOGIN "you@gmail.com"
          getCompleteResponse(110ms):
            ImapCmdResp: aaab NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)
          --getCompleteResponse
        --loginImap
        isOK:
          serverResponse: aaab NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)
        --isOK
--authenticateLogin

The answer is that your GMail account needs to be enabled for “Less Secure Apps”.

Note: Chilkat also supports OAuth2 IMAP authentication (i.e. secure apps) with GMail, and this requires an OAuth2 access token to be obtained first.  There are examples for this on example-code.com

To enable “Less Secure Apps” in your Google account, go to your Google Account Security Settings at https://myaccount.google.com/security

Scroll down and look for the “Less Secure Apps” section:

google_account_security_1

Click on “Turn on access” and then turn on access.  See below:

google_account_security_2

Re-run your program.  Now it succeeds and your LastErrorText shows this:

      authenticateLogin(250ms):
        loginImap(250ms):
          ConnectionType: SSL/TLS
          ImapCmdSent: aaab LOGIN "you@gmail.com"
          getCompleteResponse(250ms):
            ImapCmdResp: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
            ImapCmdResp: aaab OK you@gmail.com authenticated (Success)
          --getCompleteResponse
        --loginImap
        isOK:
          serverResponse: aaab OK you@gmail.com authenticated (Success)
        --isOK
      --authenticateLogin