Spring Boot 发送报警邮件

实现自动邮件发送,SpringBoot有开箱即用的工具

maven 依赖


    org.springframework.boot
    spring-boot-starter-mail

yaml 配置

spring:
  mail:
    host: smtp.163.com
    protocol: smtp
    port: 465
    username: [email protected] # 自己申请的邮箱
    password: from_stmp_pwsd #不是自己登陆密码,开启smtp/imap/pop3生成的密码
    test-connection: true
    properties:
      mail:
        imap:
          ssl:
            socketFactory:
              fallback: false
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
          ssl:
            enable: true
            socketFactory:
              port: 465
              class: javax.net.ssl.SSLSocketFactory

Java 实现类

@SpringBootTest
public class MailUtilTest {

    @Autowired
    private MailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;


    public static void main(String[] args) {
        System.out.println("hello");
    }

    @Test
    void sendMail() {
        SimpleMailMessage simpleMessage = new SimpleMailMessage();
        simpleMessage.setFrom(from);
        simpleMessage.setTo("[email protected]");
        simpleMessage.setSubject("hello world");
        simpleMessage.setText("hello world");
        mailSender.send(simpleMessage);
    }
}

参考文章

https://segmentfault.com/a/1190000021587834

你可能感兴趣的:(Spring Boot 发送报警邮件)