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 伺服器 (imap.gmail.com:993
) 和 POP 伺服器 (pop.gmail.com:995
) 時,必須使用 SSL。外寄 SMTP 伺服器 smtp.gmail.com
支援 TLS。如果用戶端以純文字開頭,請先使用通訊埠 465
(適用於 SSL) 或通訊埠 587
(適用於 TLS),再發出 STARTTLS 指令。
工作階段長度限制
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。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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."]]