Module bus.extra

Class MailAccount

java.lang.Object
org.miaixz.bus.extra.mail.MailAccount
All Implemented Interfaces:
Serializable

public class MailAccount extends Object implements Serializable
Represents a mail account, encapsulating all necessary configuration for sending emails. This includes SMTP server details, authentication credentials, and connection settings.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • MAIL_SETTING_PATHS

      public static final String[] MAIL_SETTING_PATHS
      Default paths for loading mail configuration settings.
  • Constructor Details

    • MailAccount

      public MailAccount()
      Constructs a new MailAccount with default values.
    • MailAccount

      public MailAccount(String settingPath)
      Constructs a new MailAccount from a configuration file.
      Parameters:
      settingPath - The path to the configuration file.
    • MailAccount

      public MailAccount(org.miaixz.bus.setting.Setting setting)
      Constructs a new MailAccount from a Setting object.
      Parameters:
      setting - The Setting object containing mail configuration.
  • Method Details

    • getHost

      public String getHost()
      Retrieves the SMTP server host.
      Returns:
      The SMTP server host.
    • setHost

      public MailAccount setHost(String host)
      Sets the SMTP server host.
      Parameters:
      host - The SMTP server host.
      Returns:
      This MailAccount instance for method chaining.
    • getPort

      public Integer getPort()
      Retrieves the SMTP server port.
      Returns:
      The SMTP server port.
    • setPort

      public MailAccount setPort(Integer port)
      Sets the SMTP server port.
      Parameters:
      port - The SMTP server port.
      Returns:
      This MailAccount instance for method chaining.
    • isAuth

      public Boolean isAuth()
      Checks if authentication is required.
      Returns:
      true if authentication is required, false otherwise.
    • setAuth

      public MailAccount setAuth(boolean isAuth)
      Sets whether authentication is required.
      Parameters:
      isAuth - true to enable authentication, false to disable it.
      Returns:
      This MailAccount instance for method chaining.
    • getAuthMechanisms

      public String getAuthMechanisms()
      Retrieves the authentication mechanisms.
      Returns:
      The authentication mechanisms.
    • setAuthMechanisms

      public MailAccount setAuthMechanisms(String authMechanisms)
      Sets the authentication mechanisms.
      Parameters:
      authMechanisms - The authentication mechanisms.
      Returns:
      This MailAccount instance for method chaining.
    • getUser

      public String getUser()
      Retrieves the username for authentication.
      Returns:
      The username.
    • setUser

      public MailAccount setUser(String user)
      Sets the username for authentication.
      Parameters:
      user - The username.
      Returns:
      This MailAccount instance for method chaining.
    • getPass

      public char[] getPass()
      Retrieves the password for authentication.
      Returns:
      The password as a char array.
    • setPass

      public MailAccount setPass(char[] pass)
      Sets the password for authentication.
      Parameters:
      pass - The password as a char array.
      Returns:
      This MailAccount instance for method chaining.
    • getFrom

      public String getFrom()
      Retrieves the sender's email address.
      Returns:
      The sender's email address.
    • setFrom

      public MailAccount setFrom(String from)
      Sets the sender's email address, which can be in the format "user@example.com" or "Sender Name <user@example.com>".
      Parameters:
      from - The sender's email address.
      Returns:
      This MailAccount instance for method chaining.
    • isDebug

      public boolean isDebug()
      Checks if debug mode is enabled.
      Returns:
      true if debug mode is enabled, false otherwise.
    • setDebug

      public MailAccount setDebug(boolean debug)
      Sets whether to enable debug mode.
      Parameters:
      debug - true to enable debug mode, false to disable it.
      Returns:
      This MailAccount instance for method chaining.
    • getCharset

      public Charset getCharset()
      Retrieves the character set for encoding.
      Returns:
      The character set, or null if not set.
    • setCharset

      public MailAccount setCharset(Charset charset)
      Sets the character set for encoding. If null, the global default (mail.mime.charset) is used.
      Parameters:
      charset - The character set.
      Returns:
      This MailAccount instance for method chaining.
    • isEncodefilename

      public boolean isEncodefilename()
      Checks if attachment filenames should be encoded using the specified charset.
      Returns:
      true if filenames should be encoded, false otherwise.
    • setEncodefilename

      public MailAccount setEncodefilename(boolean encodefilename)
      Sets whether to encode attachment filenames using the specified charset.
      Parameters:
      encodefilename - true to encode filenames, false to rely on system properties.
      Returns:
      This MailAccount instance for method chaining.
    • isStarttlsEnable

      public boolean isStarttlsEnable()
      Checks if STARTTLS is enabled.
      Returns:
      true if STARTTLS is enabled, false otherwise.
    • setStarttlsEnable

      public MailAccount setStarttlsEnable(boolean startttlsEnable)
      Sets whether to enable STARTTLS.
      Parameters:
      startttlsEnable - true to enable STARTTLS, false to disable it.
      Returns:
      This MailAccount instance for method chaining.
    • isSslEnable

      public Boolean isSslEnable()
      Checks if SSL is enabled.
      Returns:
      true if SSL is enabled, false otherwise.
    • setSslEnable

      public MailAccount setSslEnable(Boolean sslEnable)
      Sets whether to enable SSL.
      Parameters:
      sslEnable - true to enable SSL, false to disable it.
      Returns:
      This MailAccount instance for method chaining.
    • getSslProtocols

      public String getSslProtocols()
      Retrieves the SSL protocols to be used for the connection, separated by spaces.
      Returns:
      The SSL protocols string.
    • setSslProtocols

      public MailAccount setSslProtocols(String sslProtocols)
      Sets the SSL protocols to be used for the connection, separated by spaces.
      Parameters:
      sslProtocols - The SSL protocols string.
      Returns:
      This MailAccount instance for method chaining.
    • getSocketFactoryClass

      public String getSocketFactoryClass()
      Retrieves the socket factory class name.
      Returns:
      The socket factory class name.
    • setSocketFactoryClass

      public MailAccount setSocketFactoryClass(String socketFactoryClass)
      Sets the socket factory class name.
      Parameters:
      socketFactoryClass - The socket factory class name.
      Returns:
      This MailAccount instance for method chaining.
    • isSocketFactoryFallback

      public boolean isSocketFactoryFallback()
      Checks if socket factory fallback is enabled.
      Returns:
      true if fallback is enabled, false otherwise.
    • setSocketFactoryFallback

      public MailAccount setSocketFactoryFallback(boolean socketFactoryFallback)
      Sets whether to enable socket factory fallback.
      Parameters:
      socketFactoryFallback - true to enable fallback, false to disable it.
      Returns:
      This MailAccount instance for method chaining.
    • getSocketFactoryPort

      public int getSocketFactoryPort()
      Retrieves the socket factory port.
      Returns:
      The socket factory port.
    • setSocketFactoryPort

      public MailAccount setSocketFactoryPort(int socketFactoryPort)
      Sets the socket factory port.
      Parameters:
      socketFactoryPort - The socket factory port.
      Returns:
      This MailAccount instance for method chaining.
    • setTimeout

      public MailAccount setTimeout(long timeout)
      Sets the SMTP timeout.
      Parameters:
      timeout - The timeout in milliseconds.
      Returns:
      This MailAccount instance for method chaining.
    • setConnectionTimeout

      public MailAccount setConnectionTimeout(long connectionTimeout)
      Sets the socket connection timeout.
      Parameters:
      connectionTimeout - The timeout in milliseconds.
      Returns:
      This MailAccount instance for method chaining.
    • setWriteTimeout

      public MailAccount setWriteTimeout(long writeTimeout)
      Sets the socket write timeout.
      Parameters:
      writeTimeout - The timeout in milliseconds.
      Returns:
      This MailAccount instance for method chaining.
    • getCustomProperty

      public Map<String,Object> getCustomProperty()
      Retrieves the map of custom properties.
      Returns:
      The map of custom properties.
    • setCustomProperty

      public MailAccount setCustomProperty(String key, Object value)
      Sets a custom property.
      Parameters:
      key - The property key.
      value - The property value.
      Returns:
      This MailAccount instance for method chaining.
    • getSmtpProps

      public Properties getSmtpProps()
      Retrieves the SMTP properties for this account.
      Returns:
      A Properties object containing SMTP settings.
    • defaultIfEmpty

      public MailAccount defaultIfEmpty()
      Fills in default values for any fields that are null or blank. This is useful for creating a complete configuration from minimal information.
      Returns:
      This MailAccount instance for method chaining.
      Throws:
      NullPointerException - if the 'from' address is not set.
    • toString

      public String toString()
      Returns a string representation of the MailAccount, excluding the password for security.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the object.