Reputation: 1086
I'm getting the error below. Any idea how to fix this? Spring boot version: 3.3.0. Java version: 17.
Error:
Caused by: java.lang.IllegalStateException: No provider of jakarta.mail.util.StreamProvider was found
at jakarta.mail.util.FactoryFinder.find(FactoryFinder.java:61)
at jakarta.mail.util.StreamProvider.provider(StreamProvider.java:199)
at jakarta.mail.Session.<init>(Session.java:257)
at jakarta.mail.Session.getDefaultInstance(Session.java:383)
at jakarta.mail.Session.getDefaultInstance(Session.java:423)
Java code:
import jakarta.mail.internet.MimeMessage;
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
pom.xml:
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
Upvotes: 2
Views: 2448
Reputation: 1051
If you get the following exception
java.lang.ClassCastException: class com.sun.mail.handlers.text_html cannot be cast to class jakarta.activation.DataContentHandler (com.sun.mail.handlers.text_html and jakarta.activation.DataContentHandler are in unnamed module of loader 'app')
it means that you have a jar on your classpath (possibly com.sun.mail:mailapi) which contains /META-INF/mailcap.default
or /META-INF/mailcap
with legacy Sun packages, .e.g
text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain
text/html;; x-java-content-handler=com.sun.mail.handlers.text_html
text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml
multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mix
You need to exclude this dependency and use org.eclipse.agnus:agnus-mail instead.
Upvotes: 0
Reputation: 11065
I tried to replace the dependency, but I'm getting this error now. java.lang.ClassCastException: class com.sun.mail.handlers.text_html cannot be cast to class jakarta.activation.DataContentHandler (com.sun.mail.handlers.text_html and jakarta.activation.DataContentHandler are in unnamed module of loader 'app')
Don't mix the old activation.jar with the new jaf-api and angus-activation jars. There is a bug where mixing the old com.sun classes with the org.eclipse classes doesn't work correctly. See Using Jakarta mail and Javamail in the same runtime.
The 4 dependencies you want are:
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-activation</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
Upvotes: 1
Reputation: 9321
Eclipse angus-mail
is the successor of the sum mail impl(com.sun.mail:jakarta-mail
), replace your sun mail dependency with the following.
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
And you also need jakarta-acitivation
in the dependencies when using Jakarta Mail API.
Check my example with the custom config from scratch without Spring Boot: https://github.com/hantsy/spring6-sandbox/blob/master/mail/
But for Spring Boot users, the simplest way is to utilize mail
starter, and inject JavaMailSender
directly into your application to send an email.
Upvotes: 0