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
[email protected]
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("[email protected]"); msg.setBcc(); msg.setTo("[email protected]"); msg.setSubject("主题"); msg.setText("内容"); try { javaMailSender.send(msg); } catch (MailException ex) { System.err.println(ex.getMessage()); return false; } return true; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18565.html