Class DefaultEmailService
java.lang.Object
com.ultikits.ultitools.services.impl.DefaultEmailService
- All Implemented Interfaces:
BaseService,EmailService
Default implementation of EmailService using 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
- Specified by:
isEnabledin interfaceEmailService- Returns:
- true if email service is enabled and configured, false otherwise
-
sendEmail
Description copied from interface:EmailServiceSend a plain text email.- Specified by:
sendEmailin interfaceEmailService- Parameters:
to- recipient email addresssubject- email subjectcontent- email content (plain text)- Returns:
- true if email was sent successfully, false otherwise
-
sendEmail
Description copied from interface:EmailServiceSend a plain text email to multiple recipients.- Specified by:
sendEmailin interfaceEmailService- Parameters:
recipients- list of recipient email addressessubject- email subjectcontent- email content (plain text)- Returns:
- true if email was sent successfully to all recipients, false otherwise
-
sendHtmlEmail
Description copied from interface:EmailServiceSend an HTML email.- Specified by:
sendHtmlEmailin interfaceEmailService- Parameters:
to- recipient email addresssubject- email subjecthtmlContent- email content (HTML format)- Returns:
- true if email was sent successfully, false otherwise
-
sendHtmlEmail
Description copied from interface:EmailServiceSend an HTML email to multiple recipients.- Specified by:
sendHtmlEmailin interfaceEmailService- Parameters:
recipients- list of recipient email addressessubject- email subjecthtmlContent- email content (HTML format)- Returns:
- true if email was sent successfully to all recipients, false otherwise
-
sendEmailAsync
Description copied from interface:EmailServiceSend a plain text email asynchronously.- Specified by:
sendEmailAsyncin interfaceEmailService- Parameters:
to- recipient email addresssubject- email subjectcontent- email content (plain text)- Returns:
- CompletableFuture that completes with true if successful, false otherwise
-
sendHtmlEmailAsync
Description copied from interface:EmailServiceSend an HTML email asynchronously.- Specified by:
sendHtmlEmailAsyncin interfaceEmailService- Parameters:
to- recipient email addresssubject- email subjecthtmlContent- email content (HTML format)- Returns:
- CompletableFuture that completes with true if successful, false otherwise
-
generateVerificationCode
Description copied from interface:EmailServiceGenerate a random verification code.- Specified by:
generateVerificationCodein interfaceEmailService- Parameters:
length- length of the verification code (4-10 recommended)- 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)- 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 addressverificationCode- the verification code to sendserverName- server name to display in the emailexpiryMinutes- 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:EmailServiceSend a verification code email asynchronously.- Specified by:
sendVerificationCodeEmailAsyncin interfaceEmailService- Parameters:
to- recipient email addressverificationCode- the verification code to sendserverName- server name to display in the emailexpiryMinutes- validity period in minutes- Returns:
- CompletableFuture that completes with true if successful, false otherwise
-
testConnection
public boolean testConnection()Description copied from interface:EmailServiceTest the SMTP connection with current configuration.- Specified by:
testConnectionin interfaceEmailService- Returns:
- true if connection is successful, false otherwise
-
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
-
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
-