Integrate Jakarta Mail API 2.1.0

This commit is contained in:
Martin Bartoš 2023-03-24 12:23:37 +01:00 committed by Václav Muzikář
parent acebf4616e
commit bc43e4f435
5 changed files with 43 additions and 18 deletions

17
pom.xml
View file

@ -88,8 +88,9 @@
<jackson.annotations.version>${jackson.version}</jackson.annotations.version> <jackson.annotations.version>${jackson.version}</jackson.annotations.version>
<!--JAKARTA--> <!--JAKARTA-->
<com.sun.jakarta.mail.version>2.0.1</com.sun.jakarta.mail.version>
<jakarta.persistence.version>3.1.0</jakarta.persistence.version> <jakarta.persistence.version>3.1.0</jakarta.persistence.version>
<jakarta.mail.version>2.1.1</jakarta.mail.version>
<angus.mail.version>2.0.1</angus.mail.version>
<jakarta.servlet.version>6.0.0</jakarta.servlet.version> <jakarta.servlet.version>6.0.0</jakarta.servlet.version>
<jakarta.ws.rs.version>3.1.0</jakarta.ws.rs.version> <jakarta.ws.rs.version>3.1.0</jakarta.ws.rs.version>
<jakarta.xml.ws.version>4.0.0</jakarta.xml.ws.version> <jakarta.xml.ws.version>4.0.0</jakarta.xml.ws.version>
@ -175,7 +176,7 @@
<oracle-jdbc.version>21.5.0.0</oracle-jdbc.version> <oracle-jdbc.version>21.5.0.0</oracle-jdbc.version>
<!-- Test --> <!-- Test -->
<greenmail.version>2.0.0-alpha-3</greenmail.version> <greenmail.version>2.1.0-alpha-1</greenmail.version>
<hamcrest.version>2.2</hamcrest.version> <hamcrest.version>2.2</hamcrest.version>
<jmeter.version>2.10</jmeter.version> <jmeter.version>2.10</jmeter.version>
<junit.version>4.13.2</junit.version> <junit.version>4.13.2</junit.version>
@ -426,9 +427,15 @@
<!--JAKARTA--> <!--JAKARTA-->
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail</artifactId> <artifactId>jakarta.mail-api</artifactId>
<version>${com.sun.jakarta.mail.version}</version> <version>${jakarta.mail.version}</version>
</dependency>
<!--Implementation of the Jakarta Mail API-->
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>${angus.mail.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>jakarta.ws.rs</groupId> <groupId>jakarta.ws.rs</groupId>

View file

@ -584,8 +584,12 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail</artifactId> <artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.santuario</groupId> <groupId>org.apache.santuario</groupId>

View file

@ -56,8 +56,12 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail</artifactId> <artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>

View file

@ -17,7 +17,7 @@
package org.keycloak.email; package org.keycloak.email;
import com.sun.mail.smtp.SMTPMessage; import jakarta.mail.internet.MimeUtility;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
@ -30,18 +30,23 @@ import jakarta.mail.Address;
import jakarta.mail.MessagingException; import jakarta.mail.MessagingException;
import jakarta.mail.Multipart; import jakarta.mail.Multipart;
import jakarta.mail.Session; import jakarta.mail.Session;
import jakarta.mail.Message;
import jakarta.mail.Transport; import jakarta.mail.Transport;
import jakarta.mail.internet.AddressException; import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart; import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMultipart; import jakarta.mail.internet.MimeMultipart;
import jakarta.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static org.keycloak.utils.StringUtil.isNotBlank;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@ -123,19 +128,21 @@ public class DefaultEmailSenderProvider implements EmailSenderProvider {
multipart.addBodyPart(htmlPart); multipart.addBodyPart(htmlPart);
} }
SMTPMessage msg = new SMTPMessage(session); Message msg = new MimeMessage(session);
msg.setFrom(toInternetAddress(from, fromDisplayName)); msg.setFrom(toInternetAddress(from, fromDisplayName));
msg.setReplyTo(new Address[]{toInternetAddress(from, fromDisplayName)}); msg.setReplyTo(new Address[]{toInternetAddress(from, fromDisplayName)});
if (replyTo != null && !replyTo.isEmpty()) {
if (isNotBlank(replyTo)) {
msg.setReplyTo(new Address[]{toInternetAddress(replyTo, replyToDisplayName)}); msg.setReplyTo(new Address[]{toInternetAddress(replyTo, replyToDisplayName)});
} }
if (envelopeFrom != null && !envelopeFrom.isEmpty()) {
msg.setEnvelopeFrom(envelopeFrom); if (isNotBlank(envelopeFrom)) {
props.setProperty("mail.smtp.from", envelopeFrom);
} }
msg.setHeader("To", address); msg.setHeader("To", address);
msg.setSubject(subject, "utf-8"); msg.setSubject(MimeUtility.encodeText(subject, StandardCharsets.UTF_8.name(), null));
msg.setContent(multipart); msg.setContent(multipart);
msg.saveChanges(); msg.saveChanges();
msg.setSentDate(new Date()); msg.setSentDate(new Date());

View file

@ -36,8 +36,12 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail</artifactId> <artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
@ -285,7 +289,6 @@
<artifactId>mssql-jdbc</artifactId> <artifactId>mssql-jdbc</artifactId>
<version>${mssql-jdbc.version}</version> <version>${mssql-jdbc.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>