I am new to Jakarta Mail and trying out a standalone test program but I am get the following error
Exception in thread "main" java.lang.IllegalStateException: Not provider of jakarta.mail.util.StreamProvider
I use the following to run at the command line in Windows
C:\Progra~1\Java\jdk-21\bin\java -classpath .;jakarta.mail-api-2.1.0.jar;jakarta.activation-api-2.1.2.jar TestEmail
The program is a simple one I copied from internet.
I compiled using the following on the command line
C:\Progra~1\Java\jdk-21\bin\javac -classpath .;jakarta.mail-api-2.1.0.jar;jakarta.activation-api-2.1.2.jar TestEmail.java
import jakarta.mail.*;
import jakarta.mail.internet.*;
import java.time.*;
import java.util.*;
public class TestEmail {
public static void main(String[] args) {
String to = "sendToMailAddress";
String from = "sendFromMailAddress";
String host = "smtp.yourisp.invalid";
// Create properties, get Session
Properties props = new Properties();
Any help will be much appreciated. Peter
Because the official jakarta mail api does not provide complete implementation.
just use this one: com.sun.mail:jakarta.mail:2.0.1
is enough.
Maven
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
or download only 2 files:
C:\Progra~1\Java\jdk-21\bin\javac -classpath .;jakarta.mail-2.0.1.jar;jakarta.activation-2.0.1.jar TestEmail.java
C:\Progra~1\Java\jdk-21\bin\java -classpath .;jakarta.mail-2.0.1.jar;jakarta.activation-2.0.1.jar TestEmail
Why do some answers tell us to add jakarta.mail-api
and some answers tell us to add com.sun.mail:jakarta.mail
?
If you are developing Jakarta Web (war), then add the following:
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.3</version>
</dependency>
Run command:
mvn dependency:tree
get result:
[INFO] \- jakarta.mail:jakarta.mail-api:jar:2.1.3:compile
[INFO] \- jakarta.activation:jakarta.activation-api:jar:2.1.3:compile
then build
mvn clean package
get hello.war
(my example)
wildfly provides a complete Jakarta EE Server implementation (including mail), so only adding the above dependent content can send emails.
Tomcat does not provide a complete Jakarta EE Server implementation, so adding only the above dependencies will result in an error message:
No provider of jakarta.mail.util.StreamProvider was found</p><p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p><p><b>Exception</b></p><pre>java.lang.IllegalStateException: No provider of jakarta.mail.util.StreamProvider was found
jakarta.mail.util.FactoryFinder.find(FactoryFinder.java:61)
jakarta.mail.util.StreamProvider.provider(StreamProvider.java:199)
jakarta.mail.Session.<init>(Session.java:257)
jakarta.mail.Session.getInstance(Session.java:310)
com.example.HelloMailServlet.doGet(HelloMailServlet.java:40)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:5
Therefore, the same war file will have different results when deployed to Wildfly and Tomcat. Tomcat does not provide a complete Jakarta EE implementation, so some implementations are often missing. Therefore, we need to add additional implementations. library for tomcat war.
Since Java Application is not a Jakarta EE Server, it naturally does not have an implementation of Jakarta EE, so we directly use the jar that implements jakarta mail.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
(The War file deployed to Tomcat is also changed to add this Jar.)
please add:
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.3</version>
</dependency>
please add:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>