springboot集成qq邮箱

1.maven依赖

XML 复制代码
 <!-- email依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
java 复制代码
package com.atguigu.yygh.cmn.controller;

import lombok.Data;

import java.io.Serializable;
@Data
public class MailBean implements Serializable {
    private static final long serialVersionUID = -2116367492649751914L;
    private String recipient;//邮件接收人
    private String subject; //邮件主题
    private String content; //邮件内容
    // 省略setget方法
}
java 复制代码
package com.atguigu.yygh.cmn.controller;


import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailUtil {
    @Value("${spring.mail.username}")
    private String MAIL_SENDER; //邮件发送者

    @Autowired
    private JavaMailSender javaMailSender;

    private Logger logger = LoggerFactory.getLogger(MailUtil.class);

    /**
     * 发送文本邮件
     *
     * @param mailBean
     */
    public  void sendSimpleMail(MailBean mailBean) {
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(MAIL_SENDER);
            mailMessage.setTo(mailBean.getRecipient());
            mailMessage.setSubject(mailBean.getSubject());
            mailMessage.setText(mailBean.getContent());
            //mailMessage.copyTo(copyTo);

            javaMailSender.send(mailMessage);
        } catch (Exception e) {

        }
    }
}

配置文件

XML 复制代码
spring.mail.host=smtp.qq.com
# 邮箱地址
spring.mail.username=906533692@qq.com
# 邮箱授权码
spring.mail.password=ymfxhjieunuzbdgg
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8

测试类

java 复制代码
package com.atguigu.yygh.cmn;

import com.alibaba.excel.util.DateUtils;
import com.atguigu.yygh.cmn.controller.MailBean;
import com.atguigu.yygh.cmn.controller.MailUtil;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMailApplicationTests {

    @Autowired
    private MailUtil mailUtil;



    //接收人
    private static final String RECIPINET = "3330397915a@qq.com";

    /**
     * 发送文本邮件
     */
    @Test
    public void sendSimpleMail() {
        MailBean mailBean = new MailBean();
        mailBean.setRecipient(RECIPINET);
        mailBean.setSubject("SpringBootMail之这是一封文本的邮件");
        mailBean.setContent("SpringBootMail发送一个简单格式的邮件666666,时间为:" + DateUtils.format(new Date()));

        mailUtil.sendSimpleMail(mailBean);
    }

}

参考地址

SpringBoot发送邮件,QQ邮箱_springboot 发送qq邮件_军大君的博客-CSDN博客

相关推荐
懒羊羊不懒@2 分钟前
【MySQL | 基础】多表查询
数据库·sql·mysql
q***T5835 分钟前
前端路由懒加载实现,React与Vue
前端·vue.js·react.js
灵犀坠9 分钟前
前端开发核心知识:HTML5特性与经典面试题详解
前端·html·html5
Hilaku10 分钟前
我为什么说全栈正在杀死前端?
前端·javascript·后端
朝新_10 分钟前
【统一功能处理】SpringBoot 统一功能专题:拦截器、数据封装、异常处理及 DispatcherServlet 源码初探
java·spring boot·后端·spring·javaee
百***69711 分钟前
redis 使用
数据库·redis·缓存
mit6.82413 分钟前
[Column] 构建十亿/s级DB | 索引DB&RTDB | Kafka 为中心 | Rust 构建引擎
数据库
8***B19 分钟前
前端性能优化插件,图片懒加载与压缩
前端
木易士心30 分钟前
Vue2 和 Vue3 中 watch 用法和原理详解
前端·vue.js
q***05631 分钟前
在Mysql环境下对数据进行增删改查
数据库·mysql