1. 导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. 配置
spring.mail.username=534096094@qq.com
spring.mail.password=gtstkoszjelabijb
qq邮箱的host
spring.mail.host=smtp.qq.com
不配置可能报错
spring.mail.properties.mail.smtp.ssl.enable=true
3.使用
简单邮件
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
//邮件设置
message.setSubject("通知-今晚开会");
message.setText("今晚7:30开会");
message.setTo("17512080612@163.com");
message.setFrom("534096094@qq.com");
mailSender.send(message);
}
复杂邮件
@Test
public void test02() throws Exception{
//1、创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//邮件设置
helper.setSubject("通知-今晚开会");
//true:识别html
helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);
helper.setTo("17512080612@163.com");
helper.setFrom("534096094@qq.com");
//上传文件
helper.addAttachment("1.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\1.jpg"));
helper.addAttachment("2.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\2.jpg"));
mailSender.send(mimeMessage);
}
网友评论