引入依赖
<!--邮件模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在yml文件中配置
#邮件模块
mail:
username: 2249309153@qq.com
#授权码
password: tflgsdohacwuecde
host: smtp.qq.com
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
编写测试类
@Autowired
private JavaMailSenderImpl mailSender;
@Test
void SendMail(){
SimpleMailMessage message = new SimpleMailMessage();
//邮件设置
message.setSubject("通知-今晚开会");
message.setText("今晚7:30开会");
message.setTo("pzh2249309153@163.com");
message.setFrom("2249309153@qq.com");
mailSender.send(message);
}
复杂邮件的发送
@Test
void test02() throws MessagingException {
//创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//邮件设置
helper.setSubject("通知-今晚开会");
helper.setText("<b style='color:red'>今晚7:00开会</b>",true);
helper.setTo("pzh2249309153@163.com");
helper.setFrom("2249309153@qq.com");
//上传文件
helper.addAttachment("1.png",new File("D:\\桌面应用\\icon素材\\cart.png"));
helper.addAttachment("2.png",new File("D:\\桌面应用\\icon素材\\cart-o.png"));
mailSender.send(mimeMessage);
}