英文:
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: Added pom.xml
with some stuff removed (maven plugin)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.co.niwa.bjs</groupId>
<artifactId>mintaka-bulk-task-service</artifactId>
<version>1.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
</parent>
<properties>
<!-- versions for maven plugins -->
<java.version>17</java.version>
<jacoco.version>0.8.10</jacoco.version>
<!-- versions for build dependencies -->
<commons.collections.version>3.2.2</commons.collections.version>
<commons.io.version>2.11.0</commons.io.version>
<io.awsspring.cloud.version>3.0.1</io.awsspring.cloud.version>
<javax.measure.version>2.2</javax.measure.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<oracle.driver.version>12.1.3-0-0</oracle.driver.version>
<org.hibernate.version>
<details>
<summary>英文:</summary>
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: Added pom.xml with some stuff removed (maven plugin)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.co.niwa.bjs</groupId>
<artifactId>mintaka-bulk-task-service</artifactId>
<version>1.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
</parent>
<properties>
<!-- versions for maven plugins -->
<java.version>17</java.version>
<jacoco.version>0.8.10</jacoco.version>
<!-- versions for build dependencies -->
<commons.collections.version>3.2.2</commons.collections.version>
<commons.io.version>2.11.0</commons.io.version>
<io.awsspring.cloud.version>3.0.1</io.awsspring.cloud.version>
<javax.measure.version>2.2</javax.measure.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<oracle.driver.version>12.1.3-0-0</oracle.driver.version>
<org.hibernate.version>6.2.2.Final</org.hibernate.version>
<tech.units.ri.version>1.0.3</tech.units.ri.version>
<!-- versions for test dependencies -->
<h2.version>2.1.214</h2.version>
<squareup.okhttp3.version>4.0.1</squareup.okhttp3.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- pulling in dependencies for integrating aws with spring -->
<!-- needed for SQS and S3 logic to work -->
<!-- finding the github repo for this is a pain, to save you some time: https://github.com/awspring/spring-cloud-aws -->
<!-- the github page includes what version of io.awspring.cloud is compatible with X version of spring boot -->
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-dependencies</artifactId>
<version>${io.awsspring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter</artifactId>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-s3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- need to exclude springs default logger to use log4j2 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- needed to enable JSON logging using log4j. -->
<!-- Version implicitly provided by spring boot -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
</dependency>
<!-- Jackson - note that version is coming from spring boot -->
<!-- This is mostly to capture that we are in fact depending on these! -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<!-- Mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<!-- Database dependencies -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${org.hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.oracle.weblogic</groupId>
<artifactId>ojdbc7</artifactId>
<version>${oracle.driver.version}</version>
<scope>provided</scope>
</dependency>
<!-- Unit conversion dependencies -->
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api</artifactId>
<version>${javax.measure.version}</version>
</dependency>
<dependency>
<groupId>tec.units</groupId>
<artifactId>unit-ri</artifactId>
<version>${tech.units.ri.version}</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${squareup.okhttp3.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${squareup.okhttp3.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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] --------------------------------------------------------------
</details>
# 答案1
**得分**: 1
请使用以下方法中描述的解决方法:[665 - Jakarta Mail错误地假定类可以从Thread#getContextClassLoader加载][1],在调用期间操作上下文类加载器。
```java
@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);
};
// 发送电子邮件
Thread t = Thread.currentThread();
ClassLoader orig = t.getContextClassLoader();
t.setContextClassLoader(InternetAddress.class.getClassLoader());
try {
javaMailSender.send(preparator);
} finally {
t.setContextClassLoader(orig);
}
}
// 我假设InternetAddress.class位于与SMTP提供程序类相同的类加载器中。如果不是这种情况,那么您必须尝试其他类加载器。
[1]: https://github.com/jakartaee/mail-api/issues/665
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论