提问人:Juan C Rosero 提问时间:10/26/2023 更新时间:10/26/2023 访问量:100
尝试使用 Spring Boot API 发送电子邮件时出错:java.lang.ClassNotFoundException:javax.activation.DataContentHandler
Error While trying to send emails with a Spring Boot API : java.lang.ClassNotFoundException: javax.activation.DataContentHandler
问:
我正在尝试通过我的Spring Boot API发送电子邮件,我从maven导入了邮件启动器依赖项(我正在发送整个pom.xml以获取上下文)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>projectColocarUK</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>projectColocarUK</name>
<description>project for Colocar Courier UK</description>
<properties>
<java.version>17</java.version>
</properties>
<repositories>
<repository>
<id>central</id>
<name>Maven Central Repository</name>
<url>https://repo1.maven.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后我创建我的服务:
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String sender;
public void sendOrderConfirmationEmail(String recipient, String orderDetails) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(recipient);
helper.setSubject("New Order Registered");
helper.setText("A new order was created, Order Information is : "
+ orderDetails);
javaMailSender.send(message);
}
public void sendOrderChangeEmail(String recipient, String orderDetails) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(recipient);
helper.setSubject("There was a Change in an Order");
helper.setText("There was a change in a pending order, New Order information is : "
+ orderDetails);
javaMailSender.send(message);
}
}
在我的应用程序属性中,我配置:spring.mail.host=smtp.gmail.com spring.mail.port、spring.mail.username、spring.mail.password、spring.mail.properties.mail.smtp.auth、spring.mail.properties.mail.smtp.starttls.enable、mail.newOrders.destination(我想将通知发送到的电子邮件);
当我调用服务时:
@PostMapping
public ResponseEntity<order> create_order(@RequestBody order order){
order newOrder = order_service.create_order(order);
try {
emailService.sendOrderConfirmationEmail(receiverMail, order.getOrderDetails());
} catch (MessagingException e) {
System.out.println("Error sending confirmation email");
}
return new ResponseEntity<>(newOrder, HttpStatus.CREATED);
}
它返回以下错误:
java.lang.ClassNotFoundException: javax.activation.DataContentHandler
奇怪的是,如果我添加依赖项:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
然后它返回以下内容:
Failed message 1: java.lang.ClassCastException: class org.apache.geronimo.mail.handlers.TextHandler cannot be cast to class jakarta.activation.DataContentHandler (org.apache.geronimo.mail.handlers.TextHandler and jakarta.activation.DataContentHandler are in unnamed module of loader 'app')] with root cause
org.springframework.mail.MailSendException: Failed messages: java.lang.ClassCastException: class org.apache.geronimo.mail.handlers.TextHandler cannot be cast to class jakarta.activation.DataContentHandler (org.apache.geronimo.mail.handlers.TextHandler and jakarta.activation.DataContentHandler are in unnamed module of loader 'app')
我能做些什么来解决这个问题?
答:
1赞
Andrei Lisa
10/26/2023
#1
您遇到的主要问题是将依赖项与项目中的依赖项混合在一起以修复它,删除所有依赖项,因为您使用的是Spring Boot > 3.xjavax.
jakarta.
javax.
jakarta.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
已从 3.x 版本移至。jakarta
而不是:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
添加它:
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
</dependency>
还有一点关于你的文件:pom.xml
删除:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
而不是它,添加下一个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.1.3</version>
</dependency>
同样在您的代码中,您必须将所有导入替换为 。javax.
jakarta.
评论
1赞
Rob Spoor
10/26/2023
验证启动器的版本不是必需的(继承自父级),但最好尽可能多地使用 Spring Boot 启动器以防止版本不匹配。
1赞
Juan C Rosero
10/27/2023
谢谢,这解决了问题,在此之前我曾尝试添加 jackarta 激活依赖项,但我遇到了另一个错误(java.lang.ClassCastException:class org.apache.geronimo.mail.handlers.TextHandler 无法转换为类 jakarta.activation.DataContentHandler),我认为更改 javax 验证也与它现在的工作有关,所以感谢您的建议:)
0赞
DIVAKAR SARAGADAM
10/26/2023
#2
Link : https://central.sonatype.com/artifact/com.sun.activation/javax.activation/1.2.0?smo=true
转到该链接 添加依赖项 它会起作用
评论
0赞
ThrowsError
10/26/2023
您能否为您的解决方案添加更多信息?例如,添加解决方案的示例
评论