Class MimeBuilder

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

public final class MimeBuilder extends Object
MIME Message Builder MIME 消息构建器

Builds a complete RFC 2822 MIME message as a raw string, including all headers and a properly structured multipart body. This class replaces Jakarta Mail's MimeMessage, MimeMultipart, and MimeBodyPart.

构建完整的 RFC 2822 MIME 消息原始字符串,包括所有邮件头和正确结构化的 multipart 消息体。此类替代 Jakarta Mail 的 MimeMessageMimeMultipartMimeBodyPart

Features | 主要功能:

  • Plain text and HTML content - 纯文本和HTML内容
  • Multipart/alternative (text + HTML) - 多部分/备选(文本+HTML)
  • Regular and inline attachments - 常规和内嵌附件
  • RFC 2047 encoded headers for non-ASCII - RFC 2047 编码非ASCII邮件头
  • RFC 2822 date formatting - RFC 2822 日期格式化
  • X-Priority support - X-Priority 支持
  • Custom headers - 自定义邮件头

Message Structure | 消息结构:

Case 1: text only          → text/plain
Case 2: HTML only          → text/html
Case 3: text + HTML        → multipart/alternative { text, html }
Case 4: + attachments      → multipart/mixed { body, attachments... }
Case 5: + inline + HTML    → multipart/related { html, inline... }
Case 6: all combined       → multipart/mixed {
                                multipart/related {
                                  multipart/alternative { text, html },
                                  inline...
                                },
                                attachments...
                              }

Security | 安全性:

  • Thread-safe: Yes (stateless builder method) - 线程安全: 是(无状态构建方法)
  • Null-safe: Partial (nullable parameters documented) - 空值安全: 部分
Since:
JDK 25, opencode-base-email V1.0.3
Author:
Leon Soo
See Also:
  • Method Details

    • buildMessage

      public static String buildMessage(String from, String fromName, List<String> to, List<String> cc, List<String> bcc, String replyTo, String subject, String textContent, String htmlContent, boolean htmlFlag, String content, List<MimeBuilder.AttachmentData> attachments, Map<String,String> headers, int priority, String domain)
      Build a complete RFC 2822 MIME message 构建完整的 RFC 2822 MIME 消息

      Constructs headers and body according to the content types provided. When both textContent and htmlContent are provided, a multipart/alternative structure is created. Attachments result in multipart/mixed wrapping.

      根据提供的内容类型构建邮件头和消息体。当同时提供 textContenthtmlContent 时,创建 multipart/alternative 结构。附件导致 multipart/mixed 包装。

      Parameters:
      from - sender address ("Name <email>" or just "email") | 发件人地址
      fromName - sender display name (nullable, used for RFC 2047 encoding) | 发件人显示名称
      to - recipient addresses | 收件人地址列表
      cc - CC addresses (nullable) | 抄送地址列表
      bcc - BCC addresses (nullable) | 密送地址列表
      replyTo - reply-to address (nullable) | 回复地址
      subject - email subject | 邮件主题
      textContent - plain text body (nullable) | 纯文本正文
      htmlContent - HTML body (nullable) | HTML正文
      htmlFlag - true if content is HTML when only one content type is used | 单一内容类型时是否为HTML
      content - the main content string (used when textContent/htmlContent are not both set) | 主内容字符串
      attachments - attachment data list (nullable) | 附件数据列表
      headers - custom headers (nullable) | 自定义邮件头
      priority - X-Priority value (1=high, 3=normal, 5=low) | 优先级值
      domain - domain for Message-ID generation | 消息ID生成用的域名
      Returns:
      the complete MIME message string | 完整的 MIME 消息字符串
    • getMessageId

      public static String getMessageId(String rawMessage)
      Extract the Message-ID from a built message 从已构建的消息中提取 Message-ID
      Parameters:
      rawMessage - the raw MIME message | 原始 MIME 消息
      Returns:
      the Message-ID value, or null if not found | Message-ID 值,未找到返回 null