Class KSMTPMailer
java.lang.Object
ch.k43.util.KSMTPMailer
Compose and send MIME multipart email to an SMTP host.
Notes:
- The JAR files for jakarta.mail and angus.mail (with the activation classes) must be present in the class path during runtime.
- The default transmission is secured (STARTTLS/TLS) which can be disabled with setSecuredConnection(false)
- If no host name is specified, the highest priority MX DNS record of the first recipient will be used
to connect to the SMTP server on port 25
Example:
KSMTPMailer mailer = new KSMTPMailer();
mailer.setFrom("john.doe@acme.com");
mailer.setTo("bob.smith@hotmail.com");
mailer.setSubject("Two files");
mailer.addText("Here are the two files:");
mailer.addFile("file1.txt");
mailer.addFile("file2.txt");
mailer.addText("Regards, John");
mailer.send();
- Since:
- 2024.05.17
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a multipart file item.booleanAdd a multipart HTML item.booleanAdd a multipart text item.booleanAdd a multipart text item.Get last error message.intGet size of last message sent.booleansend()Compose and send the email multi part message.voidsetAuthentication(String argUserName, String argUserPassword) Set user name and password for basic client authentication.voidSet the blind carbon copy address(es).voidSet the carbon copy address(es).voidSet sender address.voidsetLocalFQDNHostName(String argLocalHostName) Set local FQDN host names to be used in SMTP HELO/EHLO message.voidsetOAuth2Authentication(String argUserName, String argAccessToken) Set user name and access token for OAuth 2.0 authentication.voidsetReplyTo(String argReplyToAddress) Set the reply-to address.voidsetSecureConnection(boolean argSecureConnection) Set secure (STARTTLS or TLS) or non-secure connection.
Note: The default is a secured connection.voidsetSMTPHost(String argSMTPHostName, int argSMTPHostPort) Set SMTP host name.
Note: If SMTP host name is not set, the host name from the highest priority MX record for the first recipient address (and the port number 25) is taken.voidsetSubject(String argSubject) Set email subject.voidsetSubject(String argSubject, String argCharSet) Set email subject with given character set.voidSet the recipient address(es).voidsetUnsubscribe(String argLinks) Set unsubscribe mail header.toString()String representation of object.
-
Constructor Details
-
KSMTPMailer
public KSMTPMailer()Class constructor
-
-
Method Details
-
addFile
Add a multipart file item.- Parameters:
argFileName- File name to be attached- Returns:
- True if success, false otherwise
-
addHTML
Add a multipart HTML item. The passed HTML is saved with UTF-8 charset.- Parameters:
argHTMLBody- HTML to be added- Returns:
- True if success, false otherwise
-
addText
Add a multipart text item. The passed text is saved with UTF-8 charset.- Parameters:
argTextBody- Text to be added- Returns:
- True if success, false otherwise
-
addText
-
getErrorMessage
-
getMessageSize
public int getMessageSize()Get size of last message sent.- Returns:
- Size of last message sent
- Since:
- 2024.05.27
-
send
public boolean send()Compose and send the email multi part message.If no SMTP server was previously set with setSMTPHost(), the message will be sent to the highest priority MX domain from the first recipient found.
- Returns:
- True if success, false otherwise
-
setAuthentication
-
setBCC
Set the blind carbon copy address(es).- Parameters:
argBCCAddresses- Email address(es) separated by commas
-
setCC
Set the carbon copy address(es).- Parameters:
argCCAddresses- Email address(es) separated by commas
-
setFrom
Set sender address.- Parameters:
argFromAddress- Sender email address
-
setLocalFQDNHostName
Set local FQDN host names to be used in SMTP HELO/EHLO message. If not specified, the current configured local host name is taken.- Parameters:
argLocalHostName- Local FQDN host name- Since:
- 2025.04.27
-
setOAuth2Authentication
Set user name and access token for OAuth 2.0 authentication. The access token must previously been obtained from the authorization server of the hosting provider.- Parameters:
argUserName- User nameargAccessToken- Access token- Since:
- 2024.06.11
-
setReplyTo
Set the reply-to address.- Parameters:
argReplyToAddress- Email address
-
setSecureConnection
public void setSecureConnection(boolean argSecureConnection) Set secure (STARTTLS or TLS) or non-secure connection.
Note: The default is a secured connection.- Parameters:
argSecureConnection- True for secured connection, false otherwise.
-
setSMTPHost
Set SMTP host name.
Note: If SMTP host name is not set, the host name from the highest priority MX record for the first recipient address (and the port number 25) is taken.- Parameters:
argSMTPHostName- SMTP host nameargSMTPHostPort- Host port
-
setSubject
Set email subject.- Parameters:
argSubject- Subject to be set (default is no subject)
-
setSubject
-
setTo
Set the recipient address(es).- Parameters:
argToAddresses- Email address(es) separated by commas
-
setUnsubscribe
Set unsubscribe mail header.- Parameters:
argLinks- Unsubscribe HTML link (Example<mailto:unsubscribe@acme.com>,<https://unsubscribe.acme.com>- Since:
- 2024.06.19
-
toString
-