在Sprint boot上集成一个简单的发送邮件的功能
在pom.xml
加入以下配置
1 2 3 4 5 6
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>
|
在 application.yml
文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| spring: mail: protocol: smtps host: smtp.qiye.aliyun.com username: tangyong@tangyongyong.cn password: default-encoding: UTF-8 properties: mail: smtp: auth: true starttls: enable: true socketFactory: port: 465 port: 465 socketFactoryClass: javax.net.ssl.SSLSocketFactory debug: true
|
定义服务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @Service public class SendEmail {
@Autowired private JavaMailSender mailSender;
@Test public void send(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(content); message.setFrom(from); message.setSentDate(new Date()); mailSender.send(message); } }
|