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博客

相关推荐
光影少年17 小时前
前端 AIGC
前端·aigc
启山智软17 小时前
供应链商城核心功能模块清单
java·前端·开源
我要打打代码18 小时前
关于C#线程 任务
开发语言·数据库·c#
徐同保18 小时前
Claude Code提示词案例(开发复杂动态路由详情页面)
前端
ID_1800790547318 小时前
Python调用淘宝评论API:从入门到首次采集全流程
服务器·数据库·python
uoKent18 小时前
MySQL 游标(Cursor)详解:与存储过程的结合使用
数据库·mysql
Web极客码18 小时前
宝塔面板后台突然显示“IO延迟非常高”
linux·服务器·数据库
Σdoughty18 小时前
python第三次作业
开发语言·前端·python
zhihuaba18 小时前
构建一个基于命令行的待办事项应用
jvm·数据库·python
BullSmall18 小时前
ACID 中的一致性
数据库·oracle