Spring-boot-starter-mail 3.1.1抛出“未找到jakarta.mail.util.StreamProvider的提供者”

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

提问人:petula 提问时间:7/3/2023 最后编辑:petula 更新时间:10/19/2023 访问量:1921

问:

我正在将我的项目从springboot 2.7.7升级到springboot 3.1.1,将java 11升级到17 我将spring-boot-starter-mail作为依赖项包含在内,并尝试按如下方式发送邮件

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

在javaMailSender.send,我收到以下异常:

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)"

我该如何解决这个问题?任何帮助都是值得赞赏的。 谢谢

编辑:mvn:依赖树

[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] --------------------------------------------------------------
java spring-boot 电子邮件 jakarta-mail

评论

0赞 M. Deinum 7/3/2023
请添加您的依赖项(您的 pom.xml 或 build.gradle)。
0赞 petula 7/4/2023
谢谢,我添加了pom.xml @m-deinum
0赞 M. Deinum 7/4/2023
您能提供完整的堆栈跟踪吗?您如何将其作为独立服务器运行或将其部署到其他服务器?
0赞 petula 7/6/2023
它是一个springboot应用程序,它作为独立服务器运行。将完整编辑上面的堆栈跟踪 - 谢谢
0赞 M. Deinum 7/6/2023
你能添加输出(或链接到输出的要点)吗?您正在使用 来构建可运行的 jar?而不是乱用阴影插件等自己做,或者更糟的是,从 IDE 导出?mvn dependency:treespring-boot-maven-plugin

答:

2赞 jmehrens 7/19/2023 #1

使用以下解决方法:665 - Jakarta Mail 错误地假定可以从 Thread#getContextClassLoader 加载类,这是在调用期间操作上下文类加载器。

@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);
    }

我假设 InternetAddress.class 与 SMTP 提供程序类位于同一类加载器中。如果不是这种情况,您必须尝试其他类加载器。

评论

0赞 petula 7/20/2023
非常感谢你,这对我有用 - 我希望很快就会有一个补丁!
0赞 Gabriel Bornia 10/18/2023 #2

以防万一您尝试了此处的其他评论,但仍然遇到同样的问题。

我尝试替换上下文类加载器无济于事,总是收到“找不到 jakarta.mail.util.StreamProvider 的提供者”错误消息。

在分析了依赖项之后,我注意到我正在加载的另一个库带来了 并且我包括了哪些实现。jakarta.mail:jakarta.mail-api:2.1.2sun.com.mail:jakarta.mail:2.1.0jakarta.mail:jakarta.mail-api:2.1.0

我所要做的就是确保两者都匹配(要么从另一个罐子上取下罐子,要么确保它被覆盖):

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

我删除了这里建议的类加载器代码,它有效。因此,与其调整类加载器(甚至在 https://github.com/jakartaee/mail-api/issues/665 中建议的那样),不如确保之前有正确的版本。