For non-Gmail clients, Gmail supports the standard IMAP, POP, and SMTP protocols. The Gmail IMAP, POP, and SMTP servers support authorization with the industry-standard OAuth 2.0 protocol.
Protocol
IMAP, POP, and SMTP use the standard
Simple Authentication and Security Layer (SASL),
using built-in IMAP AUTHENTICATE, POP AUTH, and SMTP AUTH commands to
authenticate users. The SASL XOAUTH2 mechanism enables clients to provide
OAuth 2.0 credentials for authentication. The
SASL XOAUTH2 protocol documentation describes the SASL
XOAUTH2 mechanism in detail, and libraries and samples are
available that implement the protocol.
Incoming connections to the IMAP server at imap.gmail.com:993 and the POP
server at pop.gmail.com:995 require SSL. The outgoing SMTP server,
smtp.gmail.com, supports TLS. If your client begins with plain text before
issuing the STARTTLS command, use port 465 (for SSL) or port 587 (for TLS).
Session length limits
Gmail POP sessions last up to approximately 7 days. Gmail IMAP sessions last up to approximately 24 hours. If you authenticated the session using OAuth credentials, it lasts approximately the validity period of the access token used (usually 1 hour). A session in this context is one continuous TCP connection.
When the time elapses and the session expires, Gmail closes the connection with a message stating that the session expired. After that, the client can reconnect, authenticate again, and continue. If using OAuth, make sure that the access token used is valid (if you try to use an access token older than 1 hour, it might be invalid).
Libraries and samples
For convenience, developers often access mail using IMAP or POP and send mail using SMTP with existing IMAP and SMTP libraries. As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail.
In addition to the SASL XOAUTH2 protocol documentation, you might also want to read Using OAuth 2.0 to Access Google APIs for more information on implementing an OAuth 2.0 client.
The OAuth 2.0 libraries and samples page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP.