Class DefaultEmailService

java.lang.Object
com.ultikits.ultitools.services.impl.DefaultEmailService
All Implemented Interfaces:
BaseService, EmailService

public class DefaultEmailService extends Object implements EmailService
Default implementation of EmailService using JavaMail.

使用JavaMail实现的默认邮件服务。

Since:
6.2.0
Version:
1.0.0
Author:
wisdomme
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • ALPHANUMERIC

      private static final String ALPHANUMERIC
      See Also:
    • NUMERIC

      private static final String NUMERIC
      See Also:
    • RANDOM

      private static final SecureRandom RANDOM
    • mailSession

      private volatile javax.mail.Session mailSession
    • lastError

      private volatile String lastError
    • initialized

      private volatile boolean initialized
  • Constructor Details

    • DefaultEmailService

      public DefaultEmailService()
  • Method Details

    • initializeSession

      private void initializeSession()
      Initialize the mail session with configuration from config.yml
    • getConfig

      private String getConfig(String path)
    • isConfigured

      private boolean isConfigured()
    • isEnabled

      public boolean isEnabled()
      Description copied from interface: EmailService
      Check if the email service is enabled and properly configured.

      Modules MUST call this method before attempting to send emails. The service is considered enabled only when:

      • email.enable is true in config.yml
      • SMTP host, username, and password are configured

      检查邮件服务是否已启用并正确配置。

      模块必须在尝试发送邮件前调用此方法。 只有在以下条件满足时服务才被视为启用:

      • config.yml中的email.enable为true
      • SMTP主机、用户名和密码已配置
      Specified by:
      isEnabled in interface EmailService
      Returns:
      true if email service is enabled and configured, false otherwise
      如果邮件服务已启用且配置正确则返回true,否则返回false
    • sendEmail

      public boolean sendEmail(String to, String subject, String content)
      Description copied from interface: EmailService
      Send a plain text email.

      发送纯文本邮件。

      Specified by:
      sendEmail in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      subject - email subject
      邮件主题
      content - email content (plain text)
      邮件内容(纯文本)
      Returns:
      true if email was sent successfully, false otherwise
      如果邮件发送成功则返回true,否则返回false
    • sendEmail

      public boolean sendEmail(List<String> recipients, String subject, String content)
      Description copied from interface: EmailService
      Send a plain text email to multiple recipients.

      发送纯文本邮件给多个收件人。

      Specified by:
      sendEmail in interface EmailService
      Parameters:
      recipients - list of recipient email addresses
      收件人邮箱地址列表
      subject - email subject
      邮件主题
      content - email content (plain text)
      邮件内容(纯文本)
      Returns:
      true if email was sent successfully to all recipients, false otherwise
      如果邮件成功发送给所有收件人则返回true,否则返回false
    • sendHtmlEmail

      public boolean sendHtmlEmail(String to, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email.

      发送HTML邮件。

      Specified by:
      sendHtmlEmail in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      subject - email subject
      邮件主题
      htmlContent - email content (HTML format)
      邮件内容(HTML格式)
      Returns:
      true if email was sent successfully, false otherwise
      如果邮件发送成功则返回true,否则返回false
    • sendHtmlEmail

      public boolean sendHtmlEmail(List<String> recipients, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email to multiple recipients.

      发送HTML邮件给多个收件人。

      Specified by:
      sendHtmlEmail in interface EmailService
      Parameters:
      recipients - list of recipient email addresses
      收件人邮箱地址列表
      subject - email subject
      邮件主题
      htmlContent - email content (HTML format)
      邮件内容(HTML格式)
      Returns:
      true if email was sent successfully to all recipients, false otherwise
      如果邮件成功发送给所有收件人则返回true,否则返回false
    • sendEmailAsync

      public CompletableFuture<Boolean> sendEmailAsync(String to, String subject, String content)
      Description copied from interface: EmailService
      Send a plain text email asynchronously.

      异步发送纯文本邮件。

      Specified by:
      sendEmailAsync in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      subject - email subject
      邮件主题
      content - email content (plain text)
      邮件内容(纯文本)
      Returns:
      CompletableFuture that completes with true if successful, false otherwise
      异步任务,成功时返回true,否则返回false
    • sendHtmlEmailAsync

      public CompletableFuture<Boolean> sendHtmlEmailAsync(String to, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email asynchronously.

      异步发送HTML邮件。

      Specified by:
      sendHtmlEmailAsync in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      subject - email subject
      邮件主题
      htmlContent - email content (HTML format)
      邮件内容(HTML格式)
      Returns:
      CompletableFuture that completes with true if successful, false otherwise
      异步任务,成功时返回true,否则返回false
    • generateVerificationCode

      public String generateVerificationCode(int length)
      Description copied from interface: EmailService
      Generate a random verification code.

      生成随机验证码。

      Specified by:
      generateVerificationCode in interface EmailService
      Parameters:
      length - length of the verification code (4-10 recommended)
      验证码长度(推荐4-10)
      Returns:
      random numeric verification code
      随机数字验证码
    • generateAlphanumericCode

      public String generateAlphanumericCode(int length)
      Description copied from interface: EmailService
      Generate a random alphanumeric verification code.

      生成随机字母数字混合验证码。

      Specified by:
      generateAlphanumericCode in interface EmailService
      Parameters:
      length - length of the verification code (4-10 recommended)
      验证码长度(推荐4-10)
      Returns:
      random alphanumeric verification code
      随机字母数字混合验证码
    • sendVerificationCodeEmail

      public boolean sendVerificationCodeEmail(String to, String verificationCode, String serverName, int expiryMinutes)
      Description copied from interface: EmailService
      Send a verification code email using a predefined template.

      使用预定义模板发送验证码邮件。

      Specified by:
      sendVerificationCodeEmail in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      verificationCode - the verification code to send
      要发送的验证码
      serverName - server name to display in the email
      要在邮件中显示的服务器名称
      expiryMinutes - validity period in minutes
      有效期(分钟)
      Returns:
      true if email was sent successfully, false otherwise
      如果邮件发送成功则返回true,否则返回false
    • sendVerificationCodeEmailAsync

      public CompletableFuture<Boolean> sendVerificationCodeEmailAsync(String to, String verificationCode, String serverName, int expiryMinutes)
      Description copied from interface: EmailService
      Send a verification code email asynchronously.

      异步发送验证码邮件。

      Specified by:
      sendVerificationCodeEmailAsync in interface EmailService
      Parameters:
      to - recipient email address
      收件人邮箱地址
      verificationCode - the verification code to send
      要发送的验证码
      serverName - server name to display in the email
      要在邮件中显示的服务器名称
      expiryMinutes - validity period in minutes
      有效期(分钟)
      Returns:
      CompletableFuture that completes with true if successful, false otherwise
      异步任务,成功时返回true,否则返回false
    • testConnection

      public boolean testConnection()
      Description copied from interface: EmailService
      Test the SMTP connection with current configuration.

      使用当前配置测试SMTP连接。

      Specified by:
      testConnection in interface EmailService
      Returns:
      true if connection is successful, false otherwise
      如果连接成功则返回true,否则返回false
    • getLastError

      public String getLastError()
      Description copied from interface: EmailService
      Get the last error message if any operation failed.

      获取上次操作失败的错误信息。

      Specified by:
      getLastError in interface EmailService
      Returns:
      last error message or null if no error
      上次错误信息,如果没有错误则返回null
    • getName

      public String getName()
      Description copied from interface: BaseService
      Get service name, this name will appear in the console or in the game.

      获取服务的名称,此名称将会出现在控制台或者游戏中

      Specified by:
      getName in interface BaseService
      Returns:
      Service name
      服务的名称
    • getAuthor

      public String getAuthor()
      Specified by:
      getAuthor in interface BaseService
      Returns:
      Author name
      作者名称
    • getVersion

      public int getVersion()
      Description copied from interface: BaseService
      Version number, used to compare the version number of the registered service, currently not in use.

      用于对比注册服务的版本号,目前没啥卵用嘿嘿嘿

      Specified by:
      getVersion in interface BaseService
      Returns:
      service version
      服务版本号
    • sendMailInternal

      private boolean sendMailInternal(String to, String subject, String content, boolean isHtml)
      Internal method to send email
    • isValidEmail

      private boolean isValidEmail(String email)
      Simple email validation
    • buildVerificationEmailTemplate

      private String buildVerificationEmailTemplate(String code, String serverName, int expiryMinutes)
      Build verification email HTML template
    • escapeHtml

      private String escapeHtml(String str)
      Escape HTML special characters