在仿做一个社区的邮件传送板块的时候,发现了一个邮件依赖版本更新的问题导致我的邮件发送功能无法使用,我的springboot版本配置为
简单直接注入JavaMailSender的时候,之后使用MimeMessageHelper传递messege在使用1.6.7版本以下的时候,是可以正常导入
javax.mail.mimemessage
但是不知道是因为什么版本问题导致控制台输出
“找不到jakarta.mail.internet.MimeMessage的类文件”
这样的错误,于是看了一下
createMimeMessage()已经变成
jakarta.mail.internet.MimeMessage
默认方法了,于是导入了jakarta 1.6.7版本以上的依赖,上面MimeMessage已经不会爆红了,但是最后输出控制台还是会报错,部分控制台错误
这是我的mailclient代码以及Test方法:
package com.example.community.util;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class MailClient {
private static final Logger logger= LoggerFactory.getLogger(MailClient.class);
@Autowired
private JavaMailSenderImpl MailSender;
@Value("${spring.mail.username}")
private String from;
public void sendMail(String to,String subject,String content){
try {
MimeMessage message=MailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
MailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
logger.error("发送邮件失败:"+e.getMessage());
}
}
}
package com.example.community;
import com.example.community.util.MailClient;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.ContextConfiguration;
import org.thymeleaf.TemplateEngine;
@SpringBootTest
public class MailTests {
@Autowired
private MailClient mailClient;
@Test
public void TestTextMail(){
//xxx我是写了具体邮箱的
mailClient.sendMail("xxx@qq.com","Test","welcome.");
}
}
正在尝试解决问题