Class SmtpEmailSender

java.lang.Object
cloud.opencode.base.email.sender.SmtpEmailSender

public class SmtpEmailSender extends Object
SMTP Email Sender SMTP邮件发送器

Email sender implementation using SMTP/SMTPS protocol.

使用SMTP/SMTPS协议的邮件发送器实现。

Features | 主要功能:

  • SMTP/SMTPS/STARTTLS support - SMTP/SMTPS/STARTTLS支持
  • Authentication support - 认证支持
  • Attachments and inline images - 附件和内嵌图片
  • HTML and plain text content - HTML和纯文本内容
  • Custom headers - 自定义邮件头

Usage Examples | 使用示例:

// Basic authentication
EmailConfig config = EmailConfig.builder()
    .host("smtp.example.com")
    .port(587)
    .username("user@example.com")
    .password("password")
    .starttls(true)
    .build();

// OAuth2 authentication (Gmail/Outlook)
EmailConfig oauthConfig = EmailConfig.builder()
    .host("smtp.gmail.com")
    .port(587)
    .username("user@gmail.com")
    .oauth2Token(accessToken)
    .starttls(true)
    .build();

SmtpEmailSender sender = new SmtpEmailSender(config);
sender.send(email);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-email V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SmtpEmailSender

      public SmtpEmailSender(EmailConfig config)
      Create SMTP sender with configuration 使用配置创建SMTP发送器
      Parameters:
      config - the email configuration | 邮件配置
  • Method Details

    • send

      public void send(Email email)
    • sendWithResult

      public SendResult sendWithResult(Email email)
    • sendBatch

      public BatchSendResult sendBatch(List<Email> emails)
      Send multiple emails using a single SMTP connection 使用单个SMTP连接发送多封邮件

      Reuses the same Transport connection for all emails in the batch, reducing TCP handshake and TLS negotiation overhead.

      对批量中所有邮件复用同一个Transport连接,减少TCP握手和TLS协商开销。

      Parameters:
      emails - the emails to send | 要发送的邮件
      Returns:
      the batch result | 批量结果
    • testConnection

      public ConnectionTestResult testConnection()
      Test SMTP server connection and authentication 测试SMTP服务器连接和认证

      Attempts to connect to the SMTP server and authenticate, then immediately disconnects. Useful for validating configuration.

      尝试连接SMTP服务器并认证,然后立即断开。用于验证配置。

      Returns:
      the test result | 测试结果
    • getConfig

      public EmailConfig getConfig()
      Get email configuration 获取邮件配置
      Returns:
      the configuration | 配置
    • close

      default void close()
      Close the sender and release resources 关闭发送器并释放资源