引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
mail:
username: 48303354@qq.com
password: XXXX
host: smtp.qq.com
properties.mail.smtp.ssl.enable: true
@SpringBootTest
public class test {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextloads(){
SimpleMailMessage message=new SimpleMailMessage();
message.setSubject("java mail测试");
message.setText("我的第一封java邮件");
message.setFrom("48303354@qq.com");
message.setTo("48303354@qq.com");
mailSender.send(message);
}
@Test
void contextloads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage=mailSender.createMimeMessage();
//组装
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
//正文
helper.setSubject("java mail测试");
helper.setText("<p style='color:red;font-size:24;text-align:center'>我的第一封java邮件</p>",true);
helper.setFrom("48303354@qq.com");
helper.setTo("48303354@qq.com");
mailSender.send(mimeMessage);
}
}