IMAP、POP 和 SMTP
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对于非 Gmail 客户端,Gmail 支持标准 IMAP、POP 和 SMTP 协议。Gmail IMAP、POP 和 SMTP 服务器已扩展为支持通过行业标准的 OAuth 2.0 协议进行授权。
协议
IMAP、POP 和 SMTP 使用标准简单身份验证和安全层 (SASL),通过内置的原生 IMAP AUTHENTICATE
、POP AUTH
和 SMTP AUTH
命令来验证用户身份。SASL XOAUTH2 机制使客户端能够提供 OAuth 2.0 凭据以进行身份验证。SASL XOAUTH2 协议文档详细介绍了 SASL XOAUTH2 机制,并提供了已实现该协议的库和示例。
对 imap.gmail.com:993
的 IMAP 服务器和 pop.gmail.com:995
的 POP 服务器的入站连接需要 SSL。发件 SMTP 服务器 smtp.gmail.com
支持 TLS。如果您的客户端以纯文本开头,请在发出 STARTTLS 命令之前使用端口 465
(对于 SSL)或端口 587
(对于 TLS)。
会话时长限制
Gmail POP 会话的期限约为 7 天。Gmail IMAP 会话的时长上限约为 24 小时。如果会话是使用 OAuth 凭据进行身份验证的,则会话的有效时间大约为所用访问令牌的有效期限(通常为 1 小时)。在此上下文中,会话是指一个连续的 TCP 连接。
当时间到期且会话过期时,Gmail 会关闭连接,并显示会话已过期的消息。之后,客户端可以重新连接、再次进行身份验证并继续。如果使用 OAuth,请确保所用的访问令牌有效(如果您尝试使用超过 1 小时的访问令牌,该令牌可能无效)。
库和示例
使用 IMAP 或 POP 访问邮件以及使用 SMTP 发送邮件通常会使用现有的 IMAP 和 SMTP 库,以方便操作。只要这些库支持简单身份验证和安全层 (SASL),它们就应该与 Gmail 支持的 SASL XOAUTH2 机制兼容。
除了 SASL XOAUTH2 协议文档之外,您可能还需要阅读使用 OAuth 2.0 访问 Google API,以详细了解如何实现 OAuth 2.0 客户端。
库和示例页面提供了多种常用语言的代码示例,这些示例使用 SASL XOAUTH2 机制与 IMAP 或 SMTP 进行交互。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# IMAP, POP, and SMTP\n\nFor non-Gmail clients, Gmail supports the standard IMAP, POP, and SMTP protocols. The Gmail IMAP, POP, and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol.\n\nProtocol\n--------\n\nIMAP, POP, and SMTP use the standard [Simple Authentication and Security Layer (SASL)](https://tools.ietf.org/html/rfc4422), using the built-in the native 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](/workspace/gmail/imap/xoauth2-protocol) describes the SASL XOAUTH2 mechanism in great detail, and [libraries and samples](/workspace/gmail/imap/xoauth2-libraries) which have implemented the protocol are available.\n\nIncoming 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).\n\nSession length limits\n---------------------\n\nGmail POP sessions are limited to about 7 days. Gmail IMAP sessions are limited to about 24 hours. If the session was authenticated using OAuth credentials, it's limited to about the validity period of the access token used (usually 1 hour). A session in this context is one continuous [TCP connection](https://en.wikipedia.org/wiki/Transmission_Control_Protocol).\n\nWhen the time elapses and the session expires, Gmail closes the connection with a message saying 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).\n\nLibraries and Samples\n---------------------\n\nAccessing mail using IMAP or POP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. As long as these libraries support the [Simple Authentication and Security Layer (SASL)](https://tools.ietf.org/html/rfc4422), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail.\n\nIn addition to the SASL XOAUTH2 [protocol documentation](/workspace/gmail/imap/xoauth2-protocol), you may also want to read [Using OAuth 2.0 to Access Google APIs](https://developers.google.com/identity/protocols/OAuth2) for further information on implementing an OAuth 2.0 client.\n\nThe [Libraries and Samples](/workspace/gmail/imap/xoauth2-libraries) page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP."]]