<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
看它引入的依赖,可以看到 jakarta.mail
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.4</version>
<scope>compile</scope>
</dependency>
# qq邮箱
spring.mail.username=xxxxxx@qq.com
# 刚刚生成的授权码
spring.mail.password=xxxxxxxxx
# qq邮箱的host
spring.mail.host=smtp.qq.com
#开启加密验证(qq邮箱)
spring.mail.properties.mail.smtp.ssl.enable=true
以QQ邮箱为例,在QQ邮箱中的设置->账户->开启pop3和smtp服务
点击开启后,会验证密保,验证通过后会弹框显示授权码
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void simpleEmailSend() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("简单邮件");
message.setText("这是一个简单的邮件");
message.setTo("xxxxxx@qq.com");
message.setFrom("xxxxxxx@qq.com");
mailSender.send(message);
}
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void complexEmailSend() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("复杂邮件");
helper.setText("<b style='color:red'>这是一个复杂的邮件</b>",true);
//发送附件
helper.addAttachment("跑步.jpg",new File("D:\photo\running.jpg"));
helper.setTo("xxxxxx@qq.com");
helper.setFrom("xxxxxx@qq.com");
mailSender.send(mimeMessage);
}