zoukankan      html  css  js  c++  java
  • springboot下实现邮件发送功能

    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了



  • 相关阅读:
    name mangling
    Haskell: What is Weak Head Normal Form
    取模运算和取余运算的区别
    a common method to rotate the image
    代码静态分析工具
    LeeCode-Single Number III
    七夕这天
    mysql TO_DAYS()
    (转)剖析Linux文件编码的查看及修改
    docker
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/8903961.html
Copyright © 2011-2022 走看看