Class MailConnection
java.lang.Object
cloud.opencode.base.email.protocol.MailConnection
- All Implemented Interfaces:
AutoCloseable
Mail Protocol Socket Connection
邮件协议套接字连接
Wraps a plain or SSL/TLS socket for mail protocol communication. Provides line-oriented I/O for SMTP, IMAP, and POP3 protocols.
封装普通或SSL/TLS套接字用于邮件协议通信。 为SMTP、IMAP和POP3协议提供面向行的I/O。
- Since:
- JDK 25, opencode-base-email V1.0.3
- Author:
- Leon Soo
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMailConnection(String host, int port, boolean ssl, Duration connectionTimeout, Duration readTimeout) Create a new mail connection 创建新的邮件连接 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidconnect()Connect to the server 连接到服务器getHost()Get the host 获取主机名Get the underlying socket's InputStream for raw reads 获取底层套接字的InputStream用于原始读取intgetPort()Get the port 获取端口booleanCheck if connected 检查是否已连接booleanisTls()Check if connection uses TLS 检查连接是否使用TLSreadExact(int count) Read exactly the specified number of characters from the server.readLine()Read a single line from the server (CRLF-terminated) 从服务器读取一行(CRLF终止)voidUpgrade the connection to TLS (STARTTLS) 升级连接到TLS(STARTTLS)voidwriteBytes(byte[] data) Write raw bytes to the server (no CRLF appended) 向服务器写入原始字节(不附加CRLF)voidWrite a line to the server (appends CRLF) 向服务器写入一行(附加CRLF)voidWrite raw string to the server (no CRLF appended) 向服务器写入原始字符串(不附加CRLF)
-
Constructor Details
-
MailConnection
public MailConnection(String host, int port, boolean ssl, Duration connectionTimeout, Duration readTimeout) Create a new mail connection 创建新的邮件连接- Parameters:
host- the server hostname | 服务器主机名port- the server port | 服务器端口ssl- true for SSL connection | true使用SSL连接connectionTimeout- connection timeout | 连接超时readTimeout- read timeout | 读取超时
-
-
Method Details
-
connect
Connect to the server 连接到服务器- Throws:
ProtocolException- if connection fails | 连接失败时抛出
-
upgradeToTls
Upgrade the connection to TLS (STARTTLS) 升级连接到TLS(STARTTLS)- Throws:
ProtocolException- if TLS upgrade fails | TLS升级失败时抛出
-
readLine
Read a single line from the server (CRLF-terminated) 从服务器读取一行(CRLF终止)- Returns:
- the line without CRLF | 不包含CRLF的行
- Throws:
ProtocolException- if read fails | 读取失败时抛出
-
writeLine
Write a line to the server (appends CRLF) 向服务器写入一行(附加CRLF)- Parameters:
line- the line to write | 要写入的行- Throws:
ProtocolException- if write fails | 写入失败时抛出
-
writeBytes
Write raw bytes to the server (no CRLF appended) 向服务器写入原始字节(不附加CRLF)- Parameters:
data- the data to write | 要写入的数据- Throws:
ProtocolException- if write fails | 写入失败时抛出
-
writeRaw
Write raw string to the server (no CRLF appended) 向服务器写入原始字符串(不附加CRLF)- Parameters:
data- the data to write | 要写入的数据- Throws:
ProtocolException- if write fails | 写入失败时抛出
-
isConnected
public boolean isConnected()Check if connected 检查是否已连接- Returns:
- true if connected | 已连接返回true
-
isTls
public boolean isTls()Check if connection uses TLS 检查连接是否使用TLS- Returns:
- true if using TLS | 使用TLS返回true
-
getInputStream
Get the underlying socket's InputStream for raw reads 获取底层套接字的InputStream用于原始读取- Returns:
- the input stream | 输入流
- Throws:
ProtocolException- if not connected | 未连接时抛出
-
readExact
Read exactly the specified number of characters from the server. Used for IMAP literal data. 从服务器精确读取指定数量的字符。用于IMAP字面量数据。Reads from the internal
BufferedReaderto ensure consistency withreadLine(). Reading directly from the raw socketInputStreamwould skip data already buffered by the reader.从内部
BufferedReader读取以确保与readLine()一致。 直接从原始套接字InputStream读取会跳过读取器已缓冲的数据。- Parameters:
count- the number of characters to read | 要读取的字符数- Returns:
- the data as a String | 数据字符串
- Throws:
ProtocolException- if read fails or EOF reached | 读取失败或达到EOF时抛出
-
getHost
-
getPort
public int getPort()Get the port 获取端口- Returns:
- the port | 端口
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-