三步搞定Springboot项目发送邮件
本文最后更新于:2023-03-13, 23:48:50
1、添加依赖
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
2、配置application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| spring: mail: host: smtp.office365.com default-encoding: utf-8 port: 587 protocol: smtp username: XXX@outlook.com password: xxxxxxxxx properties: mail: debug: true smtp: starttls: enable: true required: true socketFactory: class: javax.net.ssl.SSLSocketFactory
|
3、发送代码
PS:这是写在一个controller内部
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Resource private JavaMailSender mailSender;
@Value("${spring.mail.username}") private String from;
@GetMapping("sendEmail") public String sendEmail(){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo("666666666@qq.com"); message.setSubject("我是一个标题"); message.setText("我是一个内容"); mailSender.send(message); return "success"; }
|