pom

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

配置

  1. spring.mail.host=smtp.qq.com
  2. spring.mail.port=465
  3. spring.mail.username=from@qq.com
  4. spring.mail.password=jnliok2019
  5. spring.mail.properties.mail.smtp.ssl.enable=true
  6. spring.mail.properties.mail.smtp.auth=true
  7. spring.mail.properties.mail.smtp.starttls.enable=true
  8. spring.mail.properties.mail.smtp.starttls.required=true

Test

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(webEnvironment = WebEnvironment.NONE)
  3. public class MailTests {
  4. @Autowired
  5. private JavaMailSender mailSender;
  6. @Test
  7. public void sendSimpleMail() throws Exception {
  8. SimpleMailMessage message = new SimpleMailMessage();
  9. message.setFrom("from@qq.com");
  10. message.setTo("to@jnliok.com");
  11. message.setSubject("主题:简单邮件");
  12. message.setText("测试邮件内容");
  13. mailSender.send(message);
  14. }
  15. }