码迷,mamicode.com
首页 > 编程语言 > 详细

springboot下实现邮件发送功能

时间:2018-04-21 22:24:33      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:cli   frame   new   ttext   方法   url   set   分享   调用   

springboot给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok。

引入jar

技术分享图片
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
View Code
MailService.java
@Service
public class MailService {

     @Autowired
     private JavaMailSender mailSender; //框架自带的
    
     @Value("${spring.mail.username}")  //发送人的邮箱  比如155156641XX@163.com
  private String from;
   
     @Async  //意思是异步调用这个方法
  public void sendMail(String title, String url, String email) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from); // 发送人的邮箱
    message.setSubject(title); //标题
    message.setTo(email); //发给谁  对方邮箱
    message.setText(url); //内容
    mailSender.send(message); //发送
  } 

}

还需要在配置文件yml中写    下面的配置

spring.mail.host: smtp.163.com
spring.mail.username: 155156641xx@163.com
spring.mail.password: 填上你自己的
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true

就ok了



springboot下实现邮件发送功能

标签:cli   frame   new   ttext   方法   url   set   分享   调用   

原文地址:https://www.cnblogs.com/coder-lzh/p/8903961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!