Class SmtpClient
- All Implemented Interfaces:
AutoCloseable
Implements the Simple Mail Transfer Protocol for sending emails. Supports PLAIN, LOGIN, and XOAUTH2 authentication mechanisms, STARTTLS upgrade, and transparent dot-stuffing for message data.
实现简单邮件传输协议用于发送电子邮件。 支持PLAIN、LOGIN和XOAUTH2认证机制、STARTTLS升级以及消息数据的透明点填充。
This class is not thread-safe. Each instance manages a single SMTP connection and should be used by one thread at a time.
此类非线程安全。每个实例管理单个SMTP连接,应在同一时间仅由一个线程使用。
- Since:
- JDK 25, opencode-base-email V1.0.3
- Author:
- Leon Soo
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSmtpClient(String host, int port, boolean ssl, boolean starttls, Duration connectionTimeout, Duration readTimeout) Create a new SMTP client 创建新的SMTP客户端 -
Method Summary
Modifier and TypeMethodDescriptionvoidAuthenticate using the LOGIN mechanism 使用LOGIN机制认证voidAuthenticate using the PLAIN mechanism (RFC 4616) 使用PLAIN机制认证(RFC 4616)voidauthXOAuth2(String username, String token) Authenticate using the XOAUTH2 mechanism (for Gmail/Outlook) 使用XOAUTH2机制认证(适用于Gmail/Outlook)voidclose()Close the connection, ignoring any errors 关闭连接,忽略任何错误connect()Connect to the SMTP server and perform the EHLO handshake 连接到SMTP服务器并执行EHLO握手Get the server capabilities parsed from the EHLO response 获取从EHLO响应中解析的服务器能力集booleanhasCapability(String capability) Check if the server supports a specific capability 检查服务器是否支持指定能力booleanCheck if the underlying connection is active 检查底层连接是否活跃voidquit()Send QUIT command and close the connection 发送QUIT命令并关闭连接voidsendMessage(String sender, List<String> recipients, String rawMessage) Send a complete email message 发送完整的电子邮件
-
Constructor Details
-
SmtpClient
public SmtpClient(String host, int port, boolean ssl, boolean starttls, Duration connectionTimeout, Duration readTimeout) Create a new SMTP client 创建新的SMTP客户端- Parameters:
host- the SMTP server hostname | SMTP服务器主机名port- the SMTP server port | SMTP服务器端口ssl- true for implicit SSL/TLS connection | true使用隐式SSL/TLS连接starttls- true to upgrade via STARTTLS after connecting | true连接后通过STARTTLS升级connectionTimeout- connection timeout | 连接超时readTimeout- read timeout | 读取超时
-
-
Method Details
-
connect
Connect to the SMTP server and perform the EHLO handshake 连接到SMTP服务器并执行EHLO握手Establishes the connection, reads the server greeting, sends EHLO, and optionally upgrades to TLS via STARTTLS.
建立连接,读取服务器问候语,发送EHLO,并可选地通过STARTTLS升级到TLS。
- Returns:
- the server greeting string | 服务器问候字符串
- Throws:
ProtocolException- if connection or handshake fails | 连接或握手失败时抛出
-
authPlain
Authenticate using the PLAIN mechanism (RFC 4616) 使用PLAIN机制认证(RFC 4616)- Parameters:
username- the username | 用户名password- the password | 密码- Throws:
ProtocolException- if authentication fails | 认证失败时抛出
-
authLogin
Authenticate using the LOGIN mechanism 使用LOGIN机制认证- Parameters:
username- the username | 用户名password- the password | 密码- Throws:
ProtocolException- if authentication fails | 认证失败时抛出
-
authXOAuth2
Authenticate using the XOAUTH2 mechanism (for Gmail/Outlook) 使用XOAUTH2机制认证(适用于Gmail/Outlook)- Parameters:
username- the email address | 邮箱地址token- the OAuth2 access token | OAuth2访问令牌- Throws:
ProtocolException- if authentication fails | 认证失败时抛出
-
sendMessage
public void sendMessage(String sender, List<String> recipients, String rawMessage) throws ProtocolException Send a complete email message 发送完整的电子邮件Performs the MAIL FROM, RCPT TO, and DATA sequence. The rawMessage must be a complete RFC 2822 formatted message. Lines starting with "." are transparently dot-stuffed per RFC 5321 Section 4.5.2.
执行MAIL FROM、RCPT TO和DATA序列。 rawMessage必须是完整的RFC 2822格式消息。 以"."开头的行会根据RFC 5321第4.5.2节进行透明点填充。
- Parameters:
sender- the envelope sender address (MAIL FROM) | 信封发件人地址recipients- all envelope recipient addresses (RCPT TO) | 所有信封收件人地址rawMessage- the complete RFC 2822 message | 完整的RFC 2822消息- Throws:
ProtocolException- if sending fails | 发送失败时抛出
-
isConnected
public boolean isConnected()Check if the underlying connection is active 检查底层连接是否活跃- Returns:
- true if connected | 已连接返回true
-
quit
Send QUIT command and close the connection 发送QUIT命令并关闭连接- Throws:
ProtocolException- if the QUIT exchange fails | QUIT交换失败时抛出
-
close
public void close()Close the connection, ignoring any errors 关闭连接,忽略任何错误- Specified by:
closein interfaceAutoCloseable
-
getCapabilities
-
hasCapability
Check if the server supports a specific capability 检查服务器是否支持指定能力Matching is case-insensitive and checks only the first token of each capability line (e.g., "AUTH" matches "AUTH PLAIN LOGIN").
匹配不区分大小写,仅检查每个能力行的第一个令牌 (例如,"AUTH"匹配"AUTH PLAIN LOGIN")。
- Parameters:
capability- the capability to check (e.g., "STARTTLS", "AUTH") | 要检查的能力- Returns:
- true if the server advertises the capability | 服务器公布该能力返回true
-