[Major] Replaced javax.mail with jakarta

This commit is contained in:
Robert von Burg 2023-05-12 15:55:41 +02:00
parent 61bf44fcaf
commit 5faac2fc0e
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
6 changed files with 54 additions and 67 deletions

15
pom.xml
View File

@ -85,7 +85,8 @@
<hikaricp.version>5.0.1</hikaricp.version>
<postgresql.version>42.5.1</postgresql.version>
<antlr.version>4.9.3</antlr.version>
<javaxmail.version>1.6.2</javaxmail.version>
<jakarta-mail.version>2.1.0</jakarta-mail.version>
<angus-mail.version>2.0.1</angus-mail.version>
<csv.version>1.9.0</csv.version>
<cron.version>1.6.2</cron.version>
@ -274,14 +275,14 @@
<!-- Mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>${javaxmail.version}</version>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>${jakarta-mail.version}</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>${javaxmail.version}</version>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>${angus-mail.version}</version>
</dependency>
<!-- REST using Apache Jersey -->

View File

@ -1,6 +1,6 @@
package li.strolch.handler.mail;
import javax.mail.internet.InternetAddress;
import jakarta.mail.internet.InternetAddress;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
@ -19,24 +19,20 @@ public abstract class MailHandler extends StrolchComponent {
/**
* Sends an e-mail to given recipient
*
* @param subject
* the subject of the e-mail
* @param text
* the test of the e-mail
* @param recipients
* the comma separated list of addresses to whom to send the e-mail see {@link InternetAddress#parse(String)}
* @param subject the subject of the e-mail
* @param text the test of the e-mail
* @param recipients the comma separated list of addresses to whom to send the e-mail see
* {@link InternetAddress#parse(String)}
*/
public abstract void sendMail(String subject, String text, String recipients);
/**
* Sends an e-mail to given recipient asynchronously
*
* @param subject
* the subject of the e-mail
* @param text
* the test of the e-mail
* @param recipients
* the comma separated list of addresses to whom to send the e-mail see {@link InternetAddress#parse(String)}
* @param subject the subject of the e-mail
* @param text the test of the e-mail
* @param recipients the comma separated list of addresses to whom to send the e-mail see
* {@link InternetAddress#parse(String)}
*/
public abstract void sendMailAsync(String subject, String text, String recipients);
}

View File

@ -47,21 +47,22 @@ public class SmtpMailHandler extends MailHandler {
@Override
public void sendMailAsync(String subject, String text, String recipients) {
getExecutorService("Mail").submit(() -> {
try {
sendMail(subject, text, recipients);
} catch (Exception e) {
logger.error("Failed to send mail \"" + subject + "\" to " + recipients, e);
getExecutorService("Mail").submit(() -> doSendMail(subject, text, recipients));
}
if (hasComponent(OperationsLog.class)) {
LogMessage message = new LogMessage(this.realm, SYSTEM_USER_AGENT, getLocator(),
LogSeverity.Exception, LogMessageState.Information,
ResourceBundle.getBundle("strolch-service"), "mail.failedToSend").withException(e)
.value("reason", e).value("host", this.host).value("subject", subject)
.value("recipients", recipients);
getComponent(OperationsLog.class).addMessage(message);
}
private void doSendMail(String subject, String text, String recipients) {
try {
SmtpMailer.getInstance().sendMail(subject, text, recipients);
} catch (Exception e) {
logger.error("Failed to send mail \"" + subject + "\" to " + recipients, e);
if (hasComponent(OperationsLog.class)) {
LogMessage message = new LogMessage(this.realm, SYSTEM_USER_AGENT, getLocator(), LogSeverity.Exception,
LogMessageState.Information, ResourceBundle.getBundle("strolch-service"),
"mail.failedToSend").withException(e).value("reason", e).value("host", this.host)
.value("subject", subject).value("recipients", recipients);
getComponent(OperationsLog.class).addMessage(message);
}
});
}
}
}

View File

@ -45,12 +45,12 @@
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
</dependency>
<!-- test -->

View File

@ -1,9 +1,10 @@
package li.strolch.utils;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import jakarta.mail.*;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.*;
@ -15,7 +16,7 @@ import org.slf4j.LoggerFactory;
/**
* A simple helper class to send e-mails. Uses javax.mail and is built as a singleton, so configuration has to be done
* only once.
*
* <p>
* The {@link Properties} required are as follows:
* <ul>
* <li><code>fromAddr</code> and <code>fromName</code> - defines the address from which the e-mail comes from</li>
@ -42,8 +43,7 @@ public class SmtpMailer {
* Initializes the SMTP Mailer with the given properties.
* </p>
*
* @param properties
* the properties to be used to initialize the mailer
* @param properties the properties to be used to initialize the mailer
*/
public static void init(Properties properties) {
try {
@ -74,13 +74,10 @@ public class SmtpMailer {
/**
* private constructor, use the {@link #init(Properties)}-method
*
* @param properties
* the properties to initialize the mailer
* @param properties the properties to initialize the mailer
*
* @throws UnsupportedEncodingException
* if something goes wrong parsing the from or override addresses
* @throws AddressException
* if something goes wrong parsing the from or override addresses
* @throws UnsupportedEncodingException if something goes wrong parsing the from or override addresses
* @throws AddressException if something goes wrong parsing the from or override addresses
*/
private SmtpMailer(Properties properties) throws UnsupportedEncodingException, AddressException {
@ -127,12 +124,9 @@ public class SmtpMailer {
/**
* Sends an e-mail to given recipient (unless override address defined).
*
* @param subject
* the subject of the e-mail
* @param text
* the test of the e-mail
* @param recipients
* the addresses to whom to send the e-mail. See {@link InternetAddress#parse(String)}
* @param subject the subject of the e-mail
* @param text the test of the e-mail
* @param recipients the addresses to whom to send the e-mail. See {@link InternetAddress#parse(String)}
*/
public void sendMail(String subject, String text, String recipients) {
@ -149,8 +143,8 @@ public class SmtpMailer {
// override, with no white list, so send to override
text = "Override recipient. Original recipients: " + recipients + ".\n\n" + text;
send(subject, text, this.overrideRecipients);
logger.info(MessageFormat
.format("Sent E-mail to override recipient {0}: {1}", Arrays.stream(this.overrideRecipients) //
logger.info(MessageFormat.format("Sent E-mail to override recipient {0}: {1}",
Arrays.stream(this.overrideRecipients) //
.map(Object::toString) //
.collect(Collectors.joining(",")), subject));
@ -189,15 +183,15 @@ public class SmtpMailer {
}
} catch (MessagingException e) {
logger.error("Failed to send the following e-mail:\nSubject: " + subject + "\nRecipients: " + recipients
+ "\n\nBody:\n" + text);
logger.error("Failed to send the following e-mail:\nSubject: " + subject + "\nRecipients: " + recipients +
"\n\nBody:\n" + text);
throw new RuntimeException("Failed to send e-mail due to " + e.getMessage(), e);
}
}
private void send(String subject, String text, InternetAddress[] recipients) throws MessagingException {
Session session = Session.getInstance(this.props, new javax.mail.Authenticator() {
Session session = Session.getInstance(this.props, new jakarta.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SmtpMailer.this.username, SmtpMailer.this.password);
}

View File

@ -52,11 +52,6 @@
<artifactId>soql</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
</dependency>
<!-- Json -->
<dependency>
<groupId>com.google.code.gson</groupId>