Class DefaultEmailService
java.lang.Object
com.ultikits.ultitools.services.impl.DefaultEmailService
- All Implemented Interfaces:
BaseService,EmailService
Default implementation of EmailService using JavaMail.
使用JavaMail实现的默认邮件服务。
- Since:
- 6.2.0
- Version:
- 1.0.0
- Author:
- wisdomme
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate booleanprivate Stringprivate static final Loggerprivate javax.mail.Sessionprivate static final Stringprivate static final SecureRandom -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringbuildVerificationEmailTemplate(String code, String serverName, int expiryMinutes) Build verification email HTML templateprivate StringescapeHtml(String str) Escape HTML special charactersgenerateAlphanumericCode(int length) Generate a random alphanumeric verification code.generateVerificationCode(int length) Generate a random verification code.private StringGet the last error message if any operation failed.getName()Get service name, this name will appear in the console or in the game.intVersion number, used to compare the version number of the registered service, currently not in use.private voidInitialize the mail session with configuration from config.ymlprivate booleanbooleanCheck if the email service is enabled and properly configured.private booleanisValidEmail(String email) Simple email validationbooleanSend a plain text email.booleanSend a plain text email to multiple recipients.sendEmailAsync(String to, String subject, String content) Send a plain text email asynchronously.booleansendHtmlEmail(String to, String subject, String htmlContent) Send an HTML email.booleansendHtmlEmail(List<String> recipients, String subject, String htmlContent) Send an HTML email to multiple recipients.sendHtmlEmailAsync(String to, String subject, String htmlContent) Send an HTML email asynchronously.private booleansendMailInternal(String to, String subject, String content, boolean isHtml) Internal method to send emailbooleansendVerificationCodeEmail(String to, String verificationCode, String serverName, int expiryMinutes) Send a verification code email using a predefined template.sendVerificationCodeEmailAsync(String to, String verificationCode, String serverName, int expiryMinutes) Send a verification code email asynchronously.booleanTest the SMTP connection with current configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.ultikits.ultitools.interfaces.BaseService
getResourceFolderName
-
Field Details
-
LOGGER
-
ALPHANUMERIC
- See Also:
-
NUMERIC
- See Also:
-
RANDOM
-
mailSession
private volatile javax.mail.Session mailSession -
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
-
isConfigured
private boolean isConfigured() -
isEnabled
public boolean isEnabled()Description copied from interface:EmailServiceCheck 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:
isEnabledin interfaceEmailService- Returns:
- true if email service is enabled and configured, false otherwise
如果邮件服务已启用且配置正确则返回true,否则返回false
-
sendEmail
Description copied from interface:EmailServiceSend a plain text email.发送纯文本邮件。
- Specified by:
sendEmailin interfaceEmailService- 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
Description copied from interface:EmailServiceSend a plain text email to multiple recipients.发送纯文本邮件给多个收件人。
- Specified by:
sendEmailin interfaceEmailService- 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
Description copied from interface:EmailServiceSend an HTML email.发送HTML邮件。
- Specified by:
sendHtmlEmailin interfaceEmailService- 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
Description copied from interface:EmailServiceSend an HTML email to multiple recipients.发送HTML邮件给多个收件人。
- Specified by:
sendHtmlEmailin interfaceEmailService- 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
Description copied from interface:EmailServiceSend a plain text email asynchronously.异步发送纯文本邮件。
- Specified by:
sendEmailAsyncin interfaceEmailService- 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
Description copied from interface:EmailServiceSend an HTML email asynchronously.异步发送HTML邮件。
- Specified by:
sendHtmlEmailAsyncin interfaceEmailService- 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
Description copied from interface:EmailServiceGenerate a random verification code.生成随机验证码。
- Specified by:
generateVerificationCodein interfaceEmailService- Parameters:
length- length of the verification code (4-10 recommended)
验证码长度(推荐4-10)- Returns:
- random numeric verification code
随机数字验证码
-
generateAlphanumericCode
Description copied from interface:EmailServiceGenerate a random alphanumeric verification code.生成随机字母数字混合验证码。
- Specified by:
generateAlphanumericCodein interfaceEmailService- 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:EmailServiceSend a verification code email using a predefined template.使用预定义模板发送验证码邮件。
- Specified by:
sendVerificationCodeEmailin interfaceEmailService- 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:EmailServiceSend a verification code email asynchronously.异步发送验证码邮件。
- Specified by:
sendVerificationCodeEmailAsyncin interfaceEmailService- 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:EmailServiceTest the SMTP connection with current configuration.使用当前配置测试SMTP连接。
- Specified by:
testConnectionin interfaceEmailService- Returns:
- true if connection is successful, false otherwise
如果连接成功则返回true,否则返回false
-
getLastError
Description copied from interface:EmailServiceGet the last error message if any operation failed.获取上次操作失败的错误信息。
- Specified by:
getLastErrorin interfaceEmailService- Returns:
- last error message or null if no error
上次错误信息,如果没有错误则返回null
-
getName
Description copied from interface:BaseServiceGet service name, this name will appear in the console or in the game.获取服务的名称,此名称将会出现在控制台或者游戏中
- Specified by:
getNamein interfaceBaseService- Returns:
- Service name
服务的名称
-
getAuthor
- Specified by:
getAuthorin interfaceBaseService- Returns:
- Author name
作者名称
-
getVersion
public int getVersion()Description copied from interface:BaseServiceVersion number, used to compare the version number of the registered service, currently not in use.用于对比注册服务的版本号,目前没啥卵用嘿嘿嘿
- Specified by:
getVersionin interfaceBaseService- Returns:
- service version
服务版本号
-
sendMailInternal
Internal method to send email -
isValidEmail
Simple email validation -
buildVerificationEmailTemplate
Build verification email HTML template -
escapeHtml
Escape HTML special characters
-