Spring-boot-starter-mail 3.1.1 throws "Not provider of jakarta.mail.util.StreamProvider was found"
Asked Answered
T

3

6

I am upgrading my project from springboot 2.7.7 to springboot 3.1.1 and java 11 to 17 I have spring-boot-starter-mail included as a dependency and I try to send mail as follows

@Service
public class MailerService {

    @Autowired
    public JavaMailSender javaMailSender;

    public void sendEmail(String toAddress, String subject, String body, boolean error) throws MessagingException {

        MimeMessagePreparator preparator = mimeMessage -> {
            final Address recipient = new InternetAddress(toAddress);
            mimeMessage.setFrom(new InternetAddress(fromAddress));
            mimeMessage.setRecipient(Message.RecipientType.TO, recipient);
            mimeMessage.setSentDate(new Date());
            mimeMessage.setSubject(subject);
            mimeMessage.setText(body);
        };

        // Send the e-mail
        javaMailSender.send(preparator);

... other code...

At javaMailSender.send I get the following exception:

java.lang.IllegalStateException: Not provider of jakarta.mail.util.StreamProvider was found
0 = {StackTraceElement@19049} "org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:387)"
1 = {StackTraceElement@19050} "org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:366)"
2 = {StackTraceElement@19051} "nz.co.niwa.bjs.service.MailerService.sendEmail(MailerService.java:44)"
3 = {StackTraceElement@19052} "nz.co.niwa.bjs.service.MailerService.sendDataPointEmail(MailerService.java:54)"
4 = {StackTraceElement@19053} "nz.co.niwa.bjs.service.BulkPointDataFetchService.uploadCSVAndSendEmail(BulkPointDataFetchService.java:421)"
5 = {StackTraceElement@19054} "nz.co.niwa.bjs.service.BulkPointDataFetchService.lambda$retrieveForecastData$6(BulkPointDataFetchService.java:351)"
6 = {StackTraceElement@19055} "java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)"
7 = {StackTraceElement@19056} "java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)"
8 = {StackTraceElement@19057} "java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)"
9 = {StackTraceElement@19058} "java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)"
10 = {StackTraceElement@19059} "java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)"
11 = {StackTraceElement@19060} "java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)"
12 = {StackTraceElement@19061} "java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)"
13 = {StackTraceElement@19062} "java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)"

How can I fix this? Any help is appreciated. Thank you

EDIT: mvn:dependency tree

[INFO] --- maven-dependency-plugin:3.5.0:tree (default-cli) @ mintaka-bulk-task-service ---
[INFO] nz.co.niwa.bjs:mintaka-bulk-task-service:jar:1.6.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:3.1.1:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:3.1.1:compile
[INFO] |  +- io.micrometer:micrometer-observation:jar:1.11.1:compile
[INFO] |  |  \- io.micrometer:micrometer-commons:jar:1.11.1:compile
[INFO] |  \- io.micrometer:micrometer-core:jar:1.11.1:compile
[INFO] |     +- org.hdrhistogram:HdrHistogram:jar:2.1.12:runtime
[INFO] |     \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:3.1.1:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.15.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.1.1:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.10:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.10:compile
[INFO] |  +- org.springframework:spring-web:jar:6.0.10:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:6.0.10:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:6.0.10:compile
[INFO] |     +- org.springframework:spring-aop:jar:6.0.10:compile
[INFO] |     +- org.springframework:spring-context:jar:6.0.10:compile
[INFO] |     \- org.springframework:spring-expression:jar:6.0.10:compile
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:3.1.1:compile
[INFO] |  |  \- io.projectreactor.netty:reactor-netty-http:jar:1.1.8:compile
[INFO] |  |     +- io.netty:netty-codec-http:jar:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-common:jar:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-buffer:jar:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-transport:jar:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-codec:jar:4.1.94.Final:compile
[INFO] |  |     |  \- io.netty:netty-handler:jar:4.1.94.Final:compile
[INFO] |  |     +- io.netty:netty-codec-http2:jar:4.1.94.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns:jar:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-resolver:jar:4.1.94.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec-dns:jar:4.1.94.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns-native-macos:jar:osx-x86_64:4.1.94.Final:compile
[INFO] |  |     |  \- io.netty:netty-resolver-dns-classes-macos:jar:4.1.94.Final:compile
[INFO] |  |     +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.94.Final:compile
[INFO] |  |     |  +- io.netty:netty-transport-native-unix-common:jar:4.1.94.Final:compile
[INFO] |  |     |  \- io.netty:netty-transport-classes-epoll:jar:4.1.94.Final:compile
[INFO] |  |     \- io.projectreactor.netty:reactor-netty-core:jar:1.1.8:compile
[INFO] |  |        \- io.netty:netty-handler-proxy:jar:4.1.94.Final:compile
[INFO] |  |           \- io.netty:netty-codec-socks:jar:4.1.94.Final:compile
[INFO] |  \- org.springframework:spring-webflux:jar:6.0.10:compile
[INFO] |     \- io.projectreactor:reactor-core:jar:3.5.7:compile
[INFO] |        \- org.reactivestreams:reactive-streams:jar:1.0.4:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.1.1:compile
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.8.0:compile
[INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.0:compile
[INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:2.1.2:compile
[INFO] |  +- net.minidev:json-smart:jar:2.4.11:compile
[INFO] |  |  \- net.minidev:accessors-smart:jar:2.4.11:compile
[INFO] |  |     \- org.ow2.asm:asm:jar:9.3:compile
[INFO] |  +- org.assertj:assertj-core:jar:3.24.2:compile
[INFO] |  +- org.hamcrest:hamcrest:jar:2.2:compile
[INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.9.3:compile
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.3:compile
[INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:compile
[INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.9.3:compile
[INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:compile
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.9.3:compile
[INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.9.3:runtime
[INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.9.3:runtime
[INFO] |  +- org.mockito:mockito-core:jar:5.3.1:compile
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.14.5:compile
[INFO] |  |  \- org.objenesis:objenesis:jar:3.3:runtime
[INFO] |  +- org.mockito:mockito-junit-jupiter:jar:5.3.1:compile
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.1:compile
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:compile
[INFO] |  +- org.springframework:spring-core:jar:6.0.10:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:6.0.10:compile
[INFO] |  +- org.springframework:spring-test:jar:6.0.10:compile
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.1:compile
[INFO] +- io.awspring.cloud:spring-cloud-aws-starter:jar:3.0.1:compile
[INFO] |  +- io.awspring.cloud:spring-cloud-aws-autoconfigure:jar:3.0.1:compile
[INFO] |  +- io.awspring.cloud:spring-cloud-aws-core:jar:3.0.1:compile
[INFO] |  |  +- software.amazon.awssdk:regions:jar:2.20.63:compile
[INFO] |  |  |  +- software.amazon.awssdk:annotations:jar:2.20.63:compile
[INFO] |  |  |  +- software.amazon.awssdk:utils:jar:2.20.63:compile
[INFO] |  |  |  +- software.amazon.awssdk:sdk-core:jar:2.20.63:compile
[INFO] |  |  |  +- software.amazon.awssdk:profiles:jar:2.20.63:compile
[INFO] |  |  |  \- software.amazon.awssdk:json-utils:jar:2.20.63:compile
[INFO] |  |  |     \- software.amazon.awssdk:third-party-jackson-core:jar:2.20.63:compile
[INFO] |  |  \- software.amazon.awssdk:auth:jar:2.20.63:compile
[INFO] |  |     +- software.amazon.awssdk:http-client-spi:jar:2.20.63:compile
[INFO] |  |     \- software.amazon.eventstream:eventstream:jar:1.0.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:2.0.7:compile
[INFO] +- io.awspring.cloud:spring-cloud-aws-starter-sqs:jar:3.0.1:compile
[INFO] |  \- io.awspring.cloud:spring-cloud-aws-sqs:jar:3.0.1:compile
[INFO] |     +- software.amazon.awssdk:sqs:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:aws-query-protocol:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:protocol-core:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:aws-core:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:metrics-spi:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:endpoints-spi:jar:2.20.63:compile
[INFO] |     |  +- software.amazon.awssdk:apache-client:jar:2.20.63:runtime
[INFO] |     |  \- software.amazon.awssdk:netty-nio-client:jar:2.20.63:runtime
[INFO] |     +- software.amazon.awssdk:arns:jar:2.20.63:compile
[INFO] |     \- org.springframework:spring-messaging:jar:6.0.10:compile
[INFO] +- io.awspring.cloud:spring-cloud-aws-starter-s3:jar:3.0.1:compile
[INFO] |  +- io.awspring.cloud:spring-cloud-aws-s3:jar:3.0.1:compile
[INFO] |  |  \- software.amazon.awssdk:s3:jar:2.20.63:compile
[INFO] |  |     +- software.amazon.awssdk:aws-xml-protocol:jar:2.20.63:compile
[INFO] |  |     \- software.amazon.awssdk:crt-core:jar:2.20.63:compile
[INFO] |  \- io.awspring.cloud:spring-cloud-aws-s3-cross-region-client:jar:3.0.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:3.1.1:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.19:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.1.1:compile
[INFO] |  |  +- com.zaxxer:HikariCP:jar:5.0.1:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:6.0.10:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:3.1.1:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:3.1.1:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:6.0.10:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:6.0.10:compile
[INFO] |  \- org.springframework:spring-aspects:jar:6.0.10:compile
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:3.1.1:compile
[INFO] |  +- org.springframework:spring-context-support:jar:6.0.10:compile
[INFO] |  \- org.eclipse.angus:jakarta.mail:jar:1.1.0:compile
[INFO] |     \- org.eclipse.angus:angus-activation:jar:2.0.1:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:3.1.1:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.10:compile
[INFO] |  \- org.hibernate.validator:hibernate-validator:jar:8.0.0.Final:compile
[INFO] |     \- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:3.1.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:3.1.1:compile
[INFO] |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.33:compile
[INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:3.1.1:compile
[INFO] |  +- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.20.0:compile
[INFO] |  |  \- org.apache.logging.log4j:log4j-api:jar:2.20.0:compile
[INFO] |  +- org.apache.logging.log4j:log4j-core:jar:2.20.0:compile
[INFO] |  \- org.apache.logging.log4j:log4j-jul:jar:2.20.0:compile
[INFO] +- org.apache.logging.log4j:log4j-layout-template-json:jar:2.20.0:compile
[INFO] +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.15.2:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.15.2:compile
[INFO] +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.15.2:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.15.2:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.15.2:compile
[INFO] +- nz.co.niwa:arcgis:jar:1.3.2:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.13:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.16:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  \- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] +- nz.co.niwa:clidb:jar:1.6.8:compile
[INFO] |  +- junit:junit:jar:4.13.2:compile
[INFO] |  +- com.oracle.jdbc:ojdbc7:jar:12.1.0.2:compile
[INFO] |  |  +- com.oracle.jdbc:xdb6:jar:12.1.0.2:compile
[INFO] |  |  +- com.oracle.jdbc:orai18n:jar:12.1.0.2:compile
[INFO] |  |  +- com.oracle.jdbc:xmlparserv2:jar:12.1.0.2:compile
[INFO] |  |  +- com.oracle.jdbc:oraclepki:jar:12.1.0.2:compile
[INFO] |  |  +- com.oracle.jdbc:osdt_cert:jar:12.1.0.2:compile
[INFO] |  |  \- com.oracle.jdbc:osdt_core:jar:12.1.0.2:compile
[INFO] |  \- com.google.guava:guava:jar:30.0-jre:compile
[INFO] |     +- com.google.guava:failureaccess:jar:1.0.1:compile
[INFO] |     +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
[INFO] |     +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO] |     +- org.checkerframework:checker-qual:jar:3.5.0:compile
[INFO] |     +- com.google.errorprone:error_prone_annotations:jar:2.3.4:compile
[INFO] |     \- com.google.j2objc:j2objc-annotations:jar:1.3:compile
[INFO] +- nz.co.niwa:aquarius:jar:2.0.8:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.15:compile
[INFO] |  \- org.bouncycastle:bcpkix-jdk15on:jar:1.70:compile
[INFO] |     +- org.bouncycastle:bcprov-jdk15on:jar:1.70:compile
[INFO] |     \- org.bouncycastle:bcutil-jdk15on:jar:1.70:compile
[INFO] +- org.mapstruct:mapstruct:jar:1.4.2.Final:compile
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- commons-io:commons-io:jar:2.11.0:compile
[INFO] +- org.hibernate.orm:hibernate-core:jar:6.2.2.Final:compile
[INFO] |  +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile
[INFO] |  +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.5.1.Final:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.6.Final:runtime
[INFO] |  +- io.smallrye:jandex:jar:3.0.5:runtime
[INFO] |  +- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] |  +- net.bytebuddy:byte-buddy:jar:1.14.5:compile
[INFO] |  +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.3:runtime
[INFO] |  |  \- org.glassfish.jaxb:jaxb-core:jar:4.0.3:runtime
[INFO] |  |     +- org.glassfish.jaxb:txw2:jar:4.0.3:runtime
[INFO] |  |     \- com.sun.istack:istack-commons-runtime:jar:4.1.2:runtime
[INFO] |  +- jakarta.inject:jakarta.inject-api:jar:2.0.1:runtime
[INFO] |  \- org.antlr:antlr4-runtime:jar:4.10.1:compile
[INFO] +- com.oracle.weblogic:ojdbc7:jar:12.1.3-0-0:provided
[INFO] +- javax.measure:unit-api:jar:2.2:compile
[INFO] +- tec.units:unit-ri:jar:1.0.3:compile
[INFO] |  \- tec.uom.lib:uom-lib-common:jar:1.0.2:compile
[INFO] +- com.h2database:h2:jar:2.1.214:test
[INFO] +- com.squareup.okhttp3:okhttp:jar:4.0.1:test
[INFO] |  +- com.squareup.okio:okio:jar:2.2.2:test
[INFO] |  \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.8.22:test
[INFO] |     +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.8.22:test
[INFO] |     \- org.jetbrains:annotations:jar:13.0:test
[INFO] \- com.squareup.okhttp3:mockwebserver:jar:4.0.1:test
[INFO] --------------------------------------------------------------
Terenceterencio answered 3/7, 2023 at 2:1 Comment(7)
Please add your dependencies (your pom.xml or build.gradle).Trinette
Can you provide the full stacktrace as well? How are you running this as a standalone server or deploy it to something else?Trinette
it is a springboot application is its run as a standalone server. will edit the stacktrace above for full - thanksTerenceterencio
Could you add the output (or link to gist to the output) of mvn dependency:tree? You are using the spring-boot-maven-plugin to build the runnable jar? And not mess around with shade plugins etc. to do it yourself or worse export from the IDE?Trinette
i have added the mvn dependency:tree output, yes we use the spring-boot-maven-plugin to build the runnable jarTerenceterencio
I don't see anything obviously wrong. One thing that is weird is that your spring-boot-starter-test has a compile dependency` instead of test. Is there anything special on how you run the application?Trinette
oh no I think I have just forgotten to mark the scope of spring-boot-starter-test to test ! Application is run in a docker container but even if I run it via Intellij IDE it still gives that error when I try to send mail. Thanks anywayTerenceterencio
G
4

This issue is fixed in Jakarta Mail 2.1.3 Final Release. Upgrade Jakarta Mail to 2.1.3 and Jakarta Activation to 2.1.3.

Otherwise, use the workaround described in: 665 - Jakarta Mail erroneously assumes that classes can be loaded from Thread#getContextClassLoader which is to manipulate the context class loader during your call.

@Service
public class MailerService {

@Autowired
public JavaMailSender javaMailSender;

public void sendEmail(String toAddress, String subject, String body, boolean error) throws MessagingException {

    MimeMessagePreparator preparator = mimeMessage -> {
        final Address recipient = new InternetAddress(toAddress);
        mimeMessage.setFrom(new InternetAddress(fromAddress));
        mimeMessage.setRecipient(Message.RecipientType.TO, recipient);
        mimeMessage.setSentDate(new Date());
        mimeMessage.setSubject(subject);
        mimeMessage.setText(body);
    };

    // Send the e-mail
    Thread t = Thread.currentThread();
    ClassLoader orig = t.getContextClassLoader();
    t.setContextClassLoader(InternetAddress.class.getClassLoader());
    try {
        javaMailSender.send(preparator);
    } finally {
        t.setContextClassLoader(orig);
    }

I'm assuming that the InternetAddress.class is in the same classloader as the SMTP provider classes. If that is not the case they you have to try other classloaders.

Greengrocer answered 19/7, 2023 at 3:10 Comment(3)
Thank you so much, that worked for me - I hope there would be a patch soon!Terenceterencio
Upgrading the version didn't help me.Multiplier
@PatrykImosa Missing implementation can cause this issue too. You need to add latest versions of the 4 dependencies: JakartaMail, JAF-API, AngusMail, and AngusActivation. All need to be latest versions numbers of each.Greengrocer
M
5

Just in case you tried the other comments in here and still got the same issue.

I tried the replacement of the context class loader to no avail, always getting the "Not provider of jakarta.mail.util.StreamProvider was found" error message.

After analyzing the dependencies I noticed that another lib I was loading was bringing the jakarta.mail:jakarta.mail-api:2.1.2 and I was including sun.com.mail:jakarta.mail:2.1.0 which implements jakarta.mail:jakarta.mail-api:2.1.0.

All I had to do is make sure that both matched (either by removing the jar from the other one, or making sure it got overwritten):

        <dependency>
            <groupId>jakarta.mail</groupId>
            <artifactId>jakarta.mail-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>2.0.1</version>
        </dependency>

I removed the class loader code suggested here, and it works. So instead of tweaking the class loader (as even suggested in https://github.com/jakartaee/mail-api/issues/665), make sure you have the correct versions before.

Mortality answered 17/10, 2023 at 17:17 Comment(4)
The point is that jakarta.mail-api is only the API, and does not contain any providers. They are in jakarta-mail, which depends on jakarta.mail-api.Bowser
Future readers. Do a search like this : mvnrepository.com/search?q=jakarta.mail and then pay attention to the release dates. the "sun" one seems abandoned. the "org eclipse" one seems like it is still active. upvote for this answer, as it unblocked me.Irrupt
'javax.mail', name: 'mail', version: '1.4.7' I was updating this "old" reference. I think the old reference.. the interface (what is being called the "api" here) .. and the concrete....... were in the same single library. Now it looks like they've been split out. I am now reference "both" things. implementation group: 'jakarta.mail', name: 'jakarta.mail-api', version: jakartaMailApiVersion implementation group: 'org.eclipse.angus', name: 'jakarta.mail', version: orgEclipseAngusJakartaMailVersionIrrupt
My versions at the time I wrote this : /* the 2 entries below. the "api" seems to be the "contract". and the org-eclipse seems to be the concrete */ jakartaMailApiVersion = '2.1.3' orgEclipseAngusJakartaMailVersion = '2.0.3'Irrupt
G
4

This issue is fixed in Jakarta Mail 2.1.3 Final Release. Upgrade Jakarta Mail to 2.1.3 and Jakarta Activation to 2.1.3.

Otherwise, use the workaround described in: 665 - Jakarta Mail erroneously assumes that classes can be loaded from Thread#getContextClassLoader which is to manipulate the context class loader during your call.

@Service
public class MailerService {

@Autowired
public JavaMailSender javaMailSender;

public void sendEmail(String toAddress, String subject, String body, boolean error) throws MessagingException {

    MimeMessagePreparator preparator = mimeMessage -> {
        final Address recipient = new InternetAddress(toAddress);
        mimeMessage.setFrom(new InternetAddress(fromAddress));
        mimeMessage.setRecipient(Message.RecipientType.TO, recipient);
        mimeMessage.setSentDate(new Date());
        mimeMessage.setSubject(subject);
        mimeMessage.setText(body);
    };

    // Send the e-mail
    Thread t = Thread.currentThread();
    ClassLoader orig = t.getContextClassLoader();
    t.setContextClassLoader(InternetAddress.class.getClassLoader());
    try {
        javaMailSender.send(preparator);
    } finally {
        t.setContextClassLoader(orig);
    }

I'm assuming that the InternetAddress.class is in the same classloader as the SMTP provider classes. If that is not the case they you have to try other classloaders.

Greengrocer answered 19/7, 2023 at 3:10 Comment(3)
Thank you so much, that worked for me - I hope there would be a patch soon!Terenceterencio
Upgrading the version didn't help me.Multiplier
@PatrykImosa Missing implementation can cause this issue too. You need to add latest versions of the 4 dependencies: JakartaMail, JAF-API, AngusMail, and AngusActivation. All need to be latest versions numbers of each.Greengrocer
V
1

Just incase if you are still facing the issue. I faced this issue today and adding org.eclipse.angsus's dependency resolved this issue for me.

Actually, I don't think jakarta mail 2.1.3 latest release has fixed this issue. Keep the version at the latest and also add the below mentioned dependency of org.eclipse.angus. Jakarta mail has only the api but the actual implementation of that API is present in org.eclipse.angus. Hence it is must that you add it.

        <dependency>
            <groupId>jakarta.mail</groupId>
            <artifactId>jakarta.mail-api</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.angus</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>2.0.1</version>
        </dependency>
Valval answered 6/8 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.