Spring Boot 提供了一个发送邮件的简单抽象,使用的是 org.springframework.mail.javamail.JavaMailSender
1.添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.添加配置参数
spring.mail.host=smtp.exmail.qq.com
spring.mail.username=admin@qq.cn
spring.mail.password=123456
# 启动SSL时的配置
spring.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.smtp.socketFactory.fallback=false
spring.mail.smtp.socketFactory.port=465
3.发送邮件
@Autowired private JavaMailSender javaMailSender; @RequestMapping("/sendEmail") @ResponseBody public boolean sendEmail() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom("123@qq.cn"); msg.setBcc(); msg.setTo("admin@javastack.cn"); msg.setSubject("主题"); msg.setText("内容"); try { javaMailSender.send(msg); } catch (MailException ex) { System.err.println(ex.getMessage()); return false; } return true; }