springboot发送邮件实例

 

使用163邮箱测试springboot发送邮件,需要首先在设置里开启POP3/SMTP/IMAP,拿到授权码。

springboot发送邮件实例_第1张图片

pom


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

 

application.yml里

  mail:
    host: smtp.163.com
    username: 邮箱名称@163.com
    password: 授权码
    enable:  true
    smtp:
      auth: true
    starttls:
      enable: true
      required: true

测试controller

@RequestMapping("mail")
@RestController
public class MailController {

    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("send")
    public JSONObject send(String email){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("从哪里发出的邮件");
        simpleMailMessage.setTo(email);//发到哪个邮箱
        simpleMailMessage.setSubject("今日天气");//标题
        simpleMailMessage.setText("当前时间"+new Date()+",目前大雨");//内容
        javaMailSender.send(simpleMailMessage);

        JSONObject result =  new JSONObject();
        result.put("code","200");
        result.put("msg","发送成功");
        return result;
    }

}

 

 

 

你可能感兴趣的:(springboot)