项目可以发邮件
1.首先导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在application配置文件中进行配置
3.在Test启动类中测试
@SpringBootTest
class Springboot09TestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("xxxx你好");
mailMessage.setText("谢谢你的礼物,有空一起恰饭");
mailMessage.setTo("294453293@qq.com");
mailMessage.setFrom("294453293@qq.com");
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();//或者MimeMessage mimeMessage1 = new MimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//正文
helper.setSubject("小狂神你好呀");
helper.setText("<p>谢谢你的课程</p>",true);
//附件
helper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
helper.addAttachment("2.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
helper.setTo("24736743@qq.com");
helper.setFrom("294453293@qq.com");
mailSender.send(mimeMessage);
}
}