java.lang.Object
org.miaixz.bus.extra.mail.MailAccount
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final String[]Default paths for loading mail configuration settings. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newMailAccountwith default values.MailAccount(String settingPath) Constructs a newMailAccountfrom a configuration file.MailAccount(org.miaixz.bus.setting.Setting setting) Constructs a newMailAccountfrom aSettingobject. -
Method Summary
Modifier and TypeMethodDescriptionFills in default values for any fields that are null or blank.Retrieves the authentication mechanisms.Retrieves the character set for encoding.Retrieves the map of custom properties.getFrom()Retrieves the sender's email address.getHost()Retrieves the SMTP server host.char[]getPass()Retrieves the password for authentication.getPort()Retrieves the SMTP server port.Retrieves the SMTP properties for this account.Retrieves the socket factory class name.intRetrieves the socket factory port.Retrieves the SSL protocols to be used for the connection, separated by spaces.getUser()Retrieves the username for authentication.isAuth()Checks if authentication is required.booleanisDebug()Checks if debug mode is enabled.booleanChecks if attachment filenames should be encoded using the specified charset.booleanChecks if socket factory fallback is enabled.Checks if SSL is enabled.booleanChecks if STARTTLS is enabled.setAuth(boolean isAuth) Sets whether authentication is required.setAuthMechanisms(String authMechanisms) Sets the authentication mechanisms.setCharset(Charset charset) Sets the character set for encoding.setConnectionTimeout(long connectionTimeout) Sets the socket connection timeout.setCustomProperty(String key, Object value) Sets a custom property.setDebug(boolean debug) Sets whether to enable debug mode.setEncodefilename(boolean encodefilename) Sets whether to encode attachment filenames using the specified charset.Sets the sender's email address, which can be in the format "user@example.com" or "Sender Name <user@example.com>".Sets the SMTP server host.setPass(char[] pass) Sets the password for authentication.Sets the SMTP server port.setSocketFactoryClass(String socketFactoryClass) Sets the socket factory class name.setSocketFactoryFallback(boolean socketFactoryFallback) Sets whether to enable socket factory fallback.setSocketFactoryPort(int socketFactoryPort) Sets the socket factory port.setSslEnable(Boolean sslEnable) Sets whether to enable SSL.setSslProtocols(String sslProtocols) Sets the SSL protocols to be used for the connection, separated by spaces.setStarttlsEnable(boolean startttlsEnable) Sets whether to enable STARTTLS.setTimeout(long timeout) Sets the SMTP timeout.Sets the username for authentication.setWriteTimeout(long writeTimeout) Sets the socket write timeout.toString()Returns a string representation of the MailAccount, excluding the password for security.
-
Field Details
-
MAIL_SETTING_PATHS
Default paths for loading mail configuration settings.
-
-
Constructor Details
-
MailAccount
public MailAccount()Constructs a newMailAccountwith default values. -
MailAccount
Constructs a newMailAccountfrom a configuration file.- Parameters:
settingPath- The path to the configuration file.
-
MailAccount
public MailAccount(org.miaixz.bus.setting.Setting setting) Constructs a newMailAccountfrom aSettingobject.- Parameters:
setting- TheSettingobject containing mail configuration.
-
-
Method Details
-
getHost
Retrieves the SMTP server host.- Returns:
- The SMTP server host.
-
setHost
Sets the SMTP server host.- Parameters:
host- The SMTP server host.- Returns:
- This
MailAccountinstance for method chaining.
-
getPort
Retrieves the SMTP server port.- Returns:
- The SMTP server port.
-
setPort
Sets the SMTP server port.- Parameters:
port- The SMTP server port.- Returns:
- This
MailAccountinstance for method chaining.
-
isAuth
Checks if authentication is required.- Returns:
trueif authentication is required,falseotherwise.
-
setAuth
Sets whether authentication is required.- Parameters:
isAuth-trueto enable authentication,falseto disable it.- Returns:
- This
MailAccountinstance for method chaining.
-
getAuthMechanisms
Retrieves the authentication mechanisms.- Returns:
- The authentication mechanisms.
-
setAuthMechanisms
Sets the authentication mechanisms.- Parameters:
authMechanisms- The authentication mechanisms.- Returns:
- This
MailAccountinstance for method chaining.
-
getUser
Retrieves the username for authentication.- Returns:
- The username.
-
setUser
Sets the username for authentication.- Parameters:
user- The username.- Returns:
- This
MailAccountinstance for method chaining.
-
getPass
public char[] getPass()Retrieves the password for authentication.- Returns:
- The password as a char array.
-
setPass
Sets the password for authentication.- Parameters:
pass- The password as a char array.- Returns:
- This
MailAccountinstance for method chaining.
-
getFrom
Retrieves the sender's email address.- Returns:
- The sender's email address.
-
setFrom
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
MailAccountinstance for method chaining.
-
isDebug
public boolean isDebug()Checks if debug mode is enabled.- Returns:
trueif debug mode is enabled,falseotherwise.
-
setDebug
Sets whether to enable debug mode.- Parameters:
debug-trueto enable debug mode,falseto disable it.- Returns:
- This
MailAccountinstance for method chaining.
-
getCharset
Retrieves the character set for encoding.- Returns:
- The character set, or null if not set.
-
setCharset
Sets the character set for encoding. If null, the global default (mail.mime.charset) is used.- Parameters:
charset- The character set.- Returns:
- This
MailAccountinstance for method chaining.
-
isEncodefilename
public boolean isEncodefilename()Checks if attachment filenames should be encoded using the specified charset.- Returns:
trueif filenames should be encoded,falseotherwise.
-
setEncodefilename
Sets whether to encode attachment filenames using the specified charset.- Parameters:
encodefilename-trueto encode filenames,falseto rely on system properties.- Returns:
- This
MailAccountinstance for method chaining.
-
isStarttlsEnable
public boolean isStarttlsEnable()Checks if STARTTLS is enabled.- Returns:
trueif STARTTLS is enabled,falseotherwise.
-
setStarttlsEnable
Sets whether to enable STARTTLS.- Parameters:
startttlsEnable-trueto enable STARTTLS,falseto disable it.- Returns:
- This
MailAccountinstance for method chaining.
-
isSslEnable
Checks if SSL is enabled.- Returns:
trueif SSL is enabled,falseotherwise.
-
setSslEnable
Sets whether to enable SSL.- Parameters:
sslEnable-trueto enable SSL,falseto disable it.- Returns:
- This
MailAccountinstance for method chaining.
-
getSslProtocols
Retrieves the SSL protocols to be used for the connection, separated by spaces.- Returns:
- The SSL protocols string.
-
setSslProtocols
Sets the SSL protocols to be used for the connection, separated by spaces.- Parameters:
sslProtocols- The SSL protocols string.- Returns:
- This
MailAccountinstance for method chaining.
-
getSocketFactoryClass
Retrieves the socket factory class name.- Returns:
- The socket factory class name.
-
setSocketFactoryClass
Sets the socket factory class name.- Parameters:
socketFactoryClass- The socket factory class name.- Returns:
- This
MailAccountinstance for method chaining.
-
isSocketFactoryFallback
public boolean isSocketFactoryFallback()Checks if socket factory fallback is enabled.- Returns:
trueif fallback is enabled,falseotherwise.
-
setSocketFactoryFallback
Sets whether to enable socket factory fallback.- Parameters:
socketFactoryFallback-trueto enable fallback,falseto disable it.- Returns:
- This
MailAccountinstance for method chaining.
-
getSocketFactoryPort
public int getSocketFactoryPort()Retrieves the socket factory port.- Returns:
- The socket factory port.
-
setSocketFactoryPort
Sets the socket factory port.- Parameters:
socketFactoryPort- The socket factory port.- Returns:
- This
MailAccountinstance for method chaining.
-
setTimeout
Sets the SMTP timeout.- Parameters:
timeout- The timeout in milliseconds.- Returns:
- This
MailAccountinstance for method chaining.
-
setConnectionTimeout
Sets the socket connection timeout.- Parameters:
connectionTimeout- The timeout in milliseconds.- Returns:
- This
MailAccountinstance for method chaining.
-
setWriteTimeout
Sets the socket write timeout.- Parameters:
writeTimeout- The timeout in milliseconds.- Returns:
- This
MailAccountinstance for method chaining.
-
getCustomProperty
Retrieves the map of custom properties.- Returns:
- The map of custom properties.
-
setCustomProperty
Sets a custom property.- Parameters:
key- The property key.value- The property value.- Returns:
- This
MailAccountinstance for method chaining.
-
getSmtpProps
Retrieves the SMTP properties for this account.- Returns:
- A
Propertiesobject containing SMTP settings.
-
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
MailAccountinstance for method chaining. - Throws:
NullPointerException- if the 'from' address is not set.
-
toString
Returns a string representation of the MailAccount, excluding the password for security.
-