Class MailConnection

java.lang.Object
cloud.opencode.base.email.protocol.MailConnection
All Implemented Interfaces:
AutoCloseable

public class MailConnection extends Object implements 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

    Constructors
    Constructor
    Description
    MailConnection(String host, int port, boolean ssl, Duration connectionTimeout, Duration readTimeout)
    Create a new mail connection 创建新的邮件连接
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    Connect to the server 连接到服务器
    Get the host 获取主机名
    Get the underlying socket's InputStream for raw reads 获取底层套接字的InputStream用于原始读取
    int
    Get the port 获取端口
    boolean
    Check if connected 检查是否已连接
    boolean
    Check if connection uses TLS 检查连接是否使用TLS
    readExact(int count)
    Read exactly the specified number of characters from the server.
    Read a single line from the server (CRLF-terminated) 从服务器读取一行(CRLF终止)
    void
    Upgrade the connection to TLS (STARTTLS) 升级连接到TLS(STARTTLS)
    void
    writeBytes(byte[] data)
    Write raw bytes to the server (no CRLF appended) 向服务器写入原始字节(不附加CRLF)
    void
    Write a line to the server (appends CRLF) 向服务器写入一行(附加CRLF)
    void
    Write raw string to the server (no CRLF appended) 向服务器写入原始字符串(不附加CRLF)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public void connect() throws ProtocolException
      Connect to the server 连接到服务器
      Throws:
      ProtocolException - if connection fails | 连接失败时抛出
    • upgradeToTls

      public void upgradeToTls() throws ProtocolException
      Upgrade the connection to TLS (STARTTLS) 升级连接到TLS(STARTTLS)
      Throws:
      ProtocolException - if TLS upgrade fails | TLS升级失败时抛出
    • readLine

      public String readLine() throws ProtocolException
      Read a single line from the server (CRLF-terminated) 从服务器读取一行(CRLF终止)
      Returns:
      the line without CRLF | 不包含CRLF的行
      Throws:
      ProtocolException - if read fails | 读取失败时抛出
    • writeLine

      public void writeLine(String line) throws ProtocolException
      Write a line to the server (appends CRLF) 向服务器写入一行(附加CRLF)
      Parameters:
      line - the line to write | 要写入的行
      Throws:
      ProtocolException - if write fails | 写入失败时抛出
    • writeBytes

      public void writeBytes(byte[] data) throws ProtocolException
      Write raw bytes to the server (no CRLF appended) 向服务器写入原始字节(不附加CRLF)
      Parameters:
      data - the data to write | 要写入的数据
      Throws:
      ProtocolException - if write fails | 写入失败时抛出
    • writeRaw

      public void writeRaw(String data) throws ProtocolException
      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

      public InputStream getInputStream() throws ProtocolException
      Get the underlying socket's InputStream for raw reads 获取底层套接字的InputStream用于原始读取
      Returns:
      the input stream | 输入流
      Throws:
      ProtocolException - if not connected | 未连接时抛出
    • readExact

      public String readExact(int count) throws ProtocolException
      Read exactly the specified number of characters from the server. Used for IMAP literal data. 从服务器精确读取指定数量的字符。用于IMAP字面量数据。

      Reads from the internal BufferedReader to ensure consistency with readLine(). Reading directly from the raw socket InputStream would 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

      public String getHost()
      Get the host 获取主机名
      Returns:
      the host | 主机名
    • getPort

      public int getPort()
      Get the port 获取端口
      Returns:
      the port | 端口
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable