Spring boot下实现邮件发送功能

Springboot发送邮件其实很简单给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok。
1.引入mail jar包


            org.springframework.boot
            spring-boot-starter-mail

2.demo 实例

@Service
public class MailService {

     @Autowired
     private JavaMailSender mailSender; //框架自带的
    
     @Value("${spring.mail.username}")  //发送人的邮箱  比如[email protected]
  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); //发送
  } 

}

3.在application.properties文件中配置

spring.mail.host: smtp.163.com
spring.mail.username: [email protected]
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

就可以了

你可能感兴趣的:(spring)