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

huangapple go评论80阅读模式
英文:

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 -&gt; {
		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&quot;>
<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>

&lt;dependencyManagement&gt;
&lt;dependencies&gt;
&lt;!-- pulling in dependencies for integrating aws with spring --&gt;
&lt;!-- needed for SQS and S3 logic to work --&gt;
&lt;!-- finding the github repo for this is a pain, to save you some time:  https://github.com/awspring/spring-cloud-aws --&gt;
&lt;!-- the github page includes what version of io.awspring.cloud is compatible with X version of spring boot --&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.awspring.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-aws-dependencies&lt;/artifactId&gt;
&lt;version&gt;${io.awsspring.cloud.version}&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;scope&gt;import&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
&lt;dependencies&gt;
&lt;!-- Spring --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.awspring.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-aws-starter&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.awspring.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-aws-starter-sqs&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.awspring.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-aws-starter-s3&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
&lt;exclusions&gt;
&lt;!-- need to exclude springs default logger to use log4j2 --&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- needed to enable JSON logging using log4j. --&gt;
&lt;!-- Version implicitly provided by spring boot --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-layout-template-json&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- Jackson - note that version is coming from spring boot --&gt;
&lt;!-- This is mostly to capture that we are in fact depending on these! --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
&lt;artifactId&gt;jackson-datatype-jdk8&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
&lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
&lt;artifactId&gt;jackson-dataformat-yaml&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- Mapstruct --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;version&gt;${mapstruct.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- Apache Commons --&gt;    
&lt;dependency&gt;
&lt;groupId&gt;commons-collections&lt;/groupId&gt;
&lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
&lt;version&gt;${commons.collections.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-io&lt;/groupId&gt;
&lt;artifactId&gt;commons-io&lt;/artifactId&gt;
&lt;version&gt;${commons.io.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- Database dependencies --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate.orm&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
&lt;version&gt;${org.hibernate.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.oracle.weblogic&lt;/groupId&gt;
&lt;artifactId&gt;ojdbc7&lt;/artifactId&gt;
&lt;version&gt;${oracle.driver.version}&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!--  Unit conversion dependencies --&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.measure&lt;/groupId&gt;
&lt;artifactId&gt;unit-api&lt;/artifactId&gt;
&lt;version&gt;${javax.measure.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;tec.units&lt;/groupId&gt;
&lt;artifactId&gt;unit-ri&lt;/artifactId&gt;
&lt;version&gt;${tech.units.ri.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- Test Dependencies --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;version&gt;${h2.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
&lt;artifactId&gt;okhttp&lt;/artifactId&gt;
&lt;version&gt;${squareup.okhttp3.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
&lt;artifactId&gt;mockwebserver&lt;/artifactId&gt;
&lt;version&gt;${squareup.okhttp3.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

</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 -&gt; {
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.

huangapple
  • 本文由 发表于 2023年7月3日 10:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定