springboot 集成邮件

1:pom引入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2:直接上代码

@Component
public class EmailUtils {

    @Autowired
    private JavaMailSender javaMailSender;


    /**
     * 普通郵件
     * @param obj
     * @return
     */
    public  boolean commonEmail(JSONObject obj){
        SimpleMailMessage message = new SimpleMailMessage();
        //发送人
        message.setFrom(obj.getString("fromEmail"));
        //接收人
        message.setTo(obj.getString("toEmail"));
        //邮件主题
        message.setSubject(obj.getString("subject"));
        //邮件内容
        message.setText(obj.getString("content"));
        try {
            javaMailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
            return false;
        }
    }

}
spring:
  mail:
    #smtp服务主机  163邮箱则为smtp.163.com
    host: smtp.qq.com
    protocol: smtp
    default-encoding: UTF-8
    username: [email protected]
    password: 123123123
    test-connection: true
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

 yml配置,email服务配置,以QQ为例,进入 QQ邮箱点击设置,开启服务,提示发送短信,验证之后就会获得密码

猜你喜欢

转载自blog.csdn.net/wdz985721191/article/details/118858646
今日推荐