Springboot - JavaMail - 邮件的集成

Springboot - JavaMail - 邮件的集成


1.创建依赖

<dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-mailartifactId>
dependency>

2.配置邮件服务属性

// 模式:YAML
spring:
  mail:
    default-encoding: utf-8
    host: smtp.163.com
    port: 25
    test-connection: true
    properties:
      mail:
        smtp:
          connectiontimeout: 5000
          timeout: 3000
          writetimeout: 5000
          auth: true
    protocol: smtp
    username: m15197447018@163.com
    password: ******

3.注入JavaMailSender并发送邮件


    // 自动注入邮件发送器
    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping("/mail")
    @ResponseBody
    public String mail()throws MailException{

        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件的四要素,这是最基本的参数
        // From必须跟邮件配置里面的username一致
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        message.setSubject("Springboot - JavaMail");
        message.setText("Springboot - JavaMail 发送的测试邮件");
        mailSender.send(message);
        return "mail";
    }

你可能感兴趣的:(框架相关,-,SpringBoot体系,springboot,发送邮件,服务集成)