You can connect third-party email clients and apps to Gmail using
the standard IMAP and POP protocols to receive mail and the SMTP protocol to
send mail. All three servers support authentication and authorization using
OAuth 2.0.

## Authentication protocol

IMAP, POP, and SMTP use the standard [Simple Authentication and Security Layer
(SASL)](https://datatracker.ietf.org/doc/html/rfc4422), using built-in IMAP
`AUTHENTICATE`, POP `AUTH`, and SMTP `AUTH` commands to authenticate users. The
SASL XOAUTH2 format enables clients to provide OAuth 2.0 credentials for
authentication. The [SASL XOAUTH2 protocol
documentation](https://developers.google.com/workspace/gmail/imap/xoauth2-protocol) describes the SASL
XOAUTH2 format in detail, and [OAuth 2.0 libraries and
samples](https://developers.google.com/workspace/gmail/imap/xoauth2-libraries) are available that implement
the protocol.

## Server addresses and ports

Use the following server addresses and ports to receive and send mail:

| Protocol | Direction | Server address | Port | Encryption |
|---|---|---|---|---|
| **IMAP** | Incoming (receive) | `imap.gmail.com` | `993` | Implicit SSL/TLS |
| **POP** | Incoming (receive) | `pop.gmail.com` | `995` | Implicit SSL/TLS |
| **SMTP** | Outgoing (send) | `smtp.gmail.com` | `465` | Implicit SSL/TLS |
| **SMTP** | Outgoing (send) | `smtp.gmail.com` | `587` | TLS (`STARTTLS`) |

Incoming connections to `imap.gmail.com:993` and `pop.gmail.com:995`, as well as
outgoing connections to `smtp.gmail.com:465`, require implicit SSL/TLS. If your
SMTP client begins with plain text before issuing the `STARTTLS` command to
upgrade the connection, use `smtp.gmail.com:587`.

Use `smtp.gmail.com` to send mail from an email client or app on behalf of an
authenticated user. If you're a Google Workspace administrator routing
outgoing mail from an on-premises server, printer, scanner, or app (for example,
using `smtp-relay.gmail.com`), see [Send email from a printer, scanner, or
app](https://knowledge.workspace.google.com/admin/gmail/send-email-from-a-printer-scanner-or-app).

## 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 [Transmission Control Protocol
(TCP)](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 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 2.0, 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.

## Sending and bandwidth limits

To keep systems healthy and accounts safe, Gmail enforces daily
mail sending and bandwidth limits over a rolling 24-hour period. You can send up
to 2,000 messages per day from a paid Google Workspace account and up to
500 messages per day from a Google Workspace trial account or personal
Gmail account, with up to 100 recipients per message sent over
SMTP by POP or IMAP clients. These limits are per user and are shared across all
clients accessing the account. For complete details, see:

- [Gmail sending limits in Google Workspace](https://knowledge.workspace.google.com/admin/gmail/gmail-sending-limits-in-google-workspace)
- [Limits for sending \& getting mail](https://support.google.com/mail/answer/22839) (personal Gmail accounts)
- [Gmail bandwidth limits](https://knowledge.workspace.google.com/admin/gmail/gmail-bandwidth-limits)

## 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 [SASL](https://datatracker.ietf.org/doc/html/rfc4422),
they should be compatible with the SASL XOAUTH2 format supported by
Gmail.

In addition to the SASL XOAUTH2
[protocol documentation](https://developers.google.com/workspace/gmail/imap/xoauth2-protocol), you might also
want to read
[Using OAuth 2.0 to Access Google APIs](https://developers.google.com/identity/protocols/oauth2)
for more information on implementing an OAuth 2.0 client.

The [OAuth 2.0 libraries and samples](https://developers.google.com/workspace/gmail/imap/xoauth2-libraries)
page provides code samples in a variety of popular languages using the SASL
XOAUTH2 format with either IMAP or SMTP.