1、application.properties配置文件
#配置邮箱信息
spring.mail.username=登录的邮箱
spring.mail.password=对应授权码
#发送右键服务器
spring.mail.host=smtp.qq.com
#ssl安全连接
spring.mail.properties.smtp.ssl.enable=true
2、测试类中演示
1)发送简单邮件
@SpringBootTest
class SpringBoot12EmailApplicationTests {
@Autowired
JavaMailSenderImpl javaMailSender;
@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("放假通知!!");
message.setText("春节放假7天");
message.setFrom("发件人@qq.com");
message.setTo("收件人@163.com");
javaMailSender.send(message);
}
2)发送带附件邮件
@Test
void testMimeMail() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject("放假通知!!");
mimeMessageHelper.setText("<h2 style='color:red'>春节放假7天</h2>",true);
mimeMessageHelper.addAttachment("03.jpg",new File("E:/IdeaBackground/03.jpg"));
mimeMessageHelper.addAttachment("04.jpg",new File("E:/IdeaBackground/04.jpg"));
mimeMessageHelper.addAttachment("c.png",new File("E:/IdeaBackground/05.png"));
mimeMessageHelper.setFrom("发件人@qq.com");
mimeMessageHelper.setTo("收件人@163.com");
javaMailSender.send(mimeMessage);
}