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

springboot发送邮件

时间:2020-01-23 19:55:13      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:work   pass   ext   simple   framework   one   邮箱   start   邮件   

引入maven

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

  

yml配置

spring:
  mail:
    host: smtp.163.com    #邮件服务器地址,邮箱不一样,服务器地址不一样
    username:  #邮箱用户名 
    password:  #一般使用授权码
    properties:
      ‘mail.smtp.ssl.enable‘: ‘true‘   #设置安全连接

  

发送邮件工具类

MailUtis.java

package cn.stylefeng.guns.utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MailUtil {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    public static MailUtil mailUtil;

    @PostConstruct
    public void init(){
        mailUtil=this;
        mailUtil.javaMailSender=this.javaMailSender;
    }


    public static void sendMail(){
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setSubject("测试");   #主题
        mailMessage.setText("aaa");      #发送的内容
        mailMessage.setTo("530@qq.com");   #发送给谁邮箱地址
        mailMessage.setFrom("tfe@163.com"); #发送人,这里与yml配置的username一样
        mailUtil.javaMailSender.send(mailMessage);
    }
}

  

springboot发送邮件

标签:work   pass   ext   simple   framework   one   邮箱   start   邮件   

原文地址:https://www.cnblogs.com/pxblog/p/12231127.html

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