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.
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
      Specified by:
      isEnabled in interface EmailService
      Returns:
      true if email service is enabled and configured, false otherwise
    • 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
    • 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
    • sendHtmlEmail

      public boolean sendHtmlEmail(String to, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email.
      Specified by:
      sendHtmlEmail in interface EmailService
      Parameters:
      to - recipient email address
      subject - email subject
      htmlContent - email content (HTML format)
      Returns:
      true if email was sent successfully, false otherwise
    • sendHtmlEmail

      public boolean sendHtmlEmail(List<String> recipients, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email to multiple recipients.
      Specified by:
      sendHtmlEmail in interface EmailService
      Parameters:
      recipients - list of recipient email addresses
      subject - email subject
      htmlContent - email content (HTML format)
      Returns:
      true if email was sent successfully to all recipients, false otherwise
    • 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
    • sendHtmlEmailAsync

      public CompletableFuture<Boolean> sendHtmlEmailAsync(String to, String subject, String htmlContent)
      Description copied from interface: EmailService
      Send an HTML email asynchronously.
      Specified by:
      sendHtmlEmailAsync in interface EmailService
      Parameters:
      to - recipient email address
      subject - email subject
      htmlContent - email content (HTML format)
      Returns:
      CompletableFuture that completes with true if successful, false otherwise
    • 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)
      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)
      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
    • 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
    • testConnection

      public boolean testConnection()
      Description copied from interface: EmailService
      Test the SMTP connection with current configuration.
      Specified by:
      testConnection in interface EmailService
      Returns:
      true if connection is successful, false otherwise
    • 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
    • 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