javaspringspring-bootemailjakarta-mail

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


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] --------------------------------------------------------------

Solution

  • 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.