Class MimeEncoder

java.lang.Object
cloud.opencode.base.email.protocol.mime.MimeEncoder

public final class MimeEncoder extends Object
MIME Encoding and Decoding Utility MIME 编解码工具类

Provides RFC 2045 Base64 and Quoted-Printable encoding/decoding, RFC 2047 encoded-word encoding/decoding for message headers, and MIME boundary/message-id generation.

提供 RFC 2045 Base64 和 Quoted-Printable 编解码, RFC 2047 编码字(encoded-word)编解码用于消息头, 以及 MIME 边界/消息ID 生成。

Features | 主要功能:

  • Base64 MIME encoding (76-char line wrap) - Base64 MIME 编码(76字符换行)
  • Quoted-Printable encoding/decoding (RFC 2045) - Quoted-Printable 编解码
  • RFC 2047 encoded-word for non-ASCII headers - RFC 2047 编码字用于非ASCII邮件头
  • Unique boundary and message-id generation - 唯一边界和消息ID生成

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: No (callers must provide non-null arguments) - 空值安全: 否
Since:
JDK 25, opencode-base-email V1.0.3
Author:
Leon Soo
See Also:
  • Method Details

    • encodeBase64

      public static String encodeBase64(byte[] data)
      Encode binary data to MIME Base64 with 76-character line wrapping 将二进制数据编码为 MIME Base64(76字符换行)
      Parameters:
      data - the data to encode | 要编码的数据
      Returns:
      the Base64 encoded string | Base64 编码字符串
    • decodeBase64

      public static byte[] decodeBase64(String data)
      Decode a MIME Base64 encoded string to binary data 将 MIME Base64 编码字符串解码为二进制数据
      Parameters:
      data - the Base64 string to decode | 要解码的 Base64 字符串
      Returns:
      the decoded bytes | 解码后的字节
    • encodeQuotedPrintable

      public static String encodeQuotedPrintable(String text, String charset)
      Encode text to Quoted-Printable encoding per RFC 2045 按 RFC 2045 将文本编码为 Quoted-Printable 编码

      Non-printable characters and '=' are encoded as =XX hex pairs. Lines are soft-wrapped at 76 characters with '=' continuation.

      不可打印字符和'='被编码为 =XX 十六进制对。 行在76字符处用'='软换行。

      Parameters:
      text - the text to encode | 要编码的文本
      charset - the charset name for byte conversion | 字节转换的字符集名称
      Returns:
      the Quoted-Printable encoded string | Quoted-Printable 编码字符串
    • decodeQuotedPrintable

      public static String decodeQuotedPrintable(String text)
      Decode a Quoted-Printable encoded string 解码 Quoted-Printable 编码字符串
      Parameters:
      text - the Quoted-Printable text to decode | 要解码的 Quoted-Printable 文本
      Returns:
      the decoded string (UTF-8) | 解码后的字符串(UTF-8)
    • decodeQuotedPrintable

      public static String decodeQuotedPrintable(String text, Charset charset)
      Decode a Quoted-Printable encoded string with the specified charset 使用指定字符集解码 Quoted-Printable 编码字符串
      Parameters:
      text - the Quoted-Printable text to decode | 要解码的 Quoted-Printable 文本
      charset - the charset for decoding | 解码字符集
      Returns:
      the decoded string | 解码后的字符串
    • encodeWord

      public static String encodeWord(String word, String charset)
      Encode a header word using RFC 2047 Base64 encoding 使用 RFC 2047 Base64 编码对邮件头字词进行编码

      Produces encoded-words of the form =?charset?B?base64?=. Only encodes if the word contains non-ASCII characters.

      生成形如 =?charset?B?base64?= 的编码字。 仅在字词包含非ASCII字符时进行编码。

      Parameters:
      word - the word to encode | 要编码的字词
      charset - the charset name (e.g., "UTF-8") | 字符集名称
      Returns:
      the RFC 2047 encoded word, or the original if pure ASCII | 编码后的字词,纯ASCII则返回原文
    • decodeWord

      public static String decodeWord(String encoded)
      Decode an RFC 2047 encoded-word string 解码 RFC 2047 编码字字符串

      Handles both B (Base64) and Q (Quoted-Printable) encodings. Non-encoded text is returned as-is.

      支持 B(Base64)和 Q(Quoted-Printable)编码。 非编码文本原样返回。

      Parameters:
      encoded - the encoded string (may contain multiple encoded-words) | 编码字符串
      Returns:
      the decoded string | 解码后的字符串
    • generateBoundary

      public static String generateBoundary()
      Generate a unique MIME boundary string 生成唯一的 MIME 边界字符串
      Returns:
      the boundary string (without leading "--") | 边界字符串(不含前导"--")
    • generateMessageId

      public static String generateMessageId(String domain)
      Generate a unique Message-ID header value 生成唯一的 Message-ID 邮件头值
      Parameters:
      domain - the domain for the message-id (e.g., "example.com") | 消息ID的域名
      Returns:
      the message-id in angle brackets (e.g., "<uuid@domain>") | 尖括号内的消息ID