注意:绿色标注的163邮箱必须一致
1.依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 2.配置 spring.mail.host=smtp.163.com spring.mail.username=xxx@163.com spring.mail.password=密码或授权码 3.发送代码 @Autowired private MailSender mailSender; @GetMapping("/email") public String sendEmail() throws MessagingException { String to = "xxx@qq.com"; String subject = "Test"; String text = "This is a test from spring boot"; SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom("xxx@163.com"); simpleMailMessage.setSubject(subject); simpleMailMessage.setTo(to); simpleMailMessage.setText(text); mailSender.send(simpleMailMessage); return "done"; }
相互学习,共同进步!