当前位置: 首页 > article >正文

SpringBoot项目发送邮件

📑前言

本文主要是【SpringBoot】——SpringBoot项目发送邮件的文章,如果有什么需要改进的地方还请大佬指出⛺️

🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见

目录

    • 📑前言
  • SpringBoot项目发送邮件
    • springboot整合mail发送邮件
    • 1.在pom.xml中导入邮件发送依赖
    • 2.配置yml文件中mail的信息
    • 3.编写邮件发送类EmailSending
    • 4.编写测试类EmailSendApplicationTests
    • 📑文章末尾

SpringBoot项目发送邮件

springboot整合mail发送邮件

1.在pom.xml中导入邮件发送依赖

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

2.配置yml文件中mail的信息

  mail:
    host: smtp.163.com #邮箱采用的是网易邮箱,也可以更换其他的邮箱
    username: 15671190765@163.com
    password: xxxx  #配置邮箱的snmp验证信息

3.编写邮件发送类EmailSending

package com.emailsend.listener;

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;

import java.util.Random;

@Component
public class EmailSending {

    @Autowired
    JavaMailSender sender;

    @Value("${spring.mail.username}")
    String username;
    private SimpleMailMessage createMessage(String title, String content, String email){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(title);  //主题
        message.setText(content);   //内容
        message.setTo(email);       //发送目标邮箱
        message.setFrom(username);  //源发送邮箱
        return message;
    }
    public void sendMailMessage(String email){
        Random random = new Random();
        int code = random.nextInt(899999)+100000;
        SimpleMailMessage  message= this.createMessage("欢迎注册我们的网站","您的验证码为"+(code)+",有效时间三分钟,为了保障您的安全,请勿向他人泄露验证码信息。",email);
        if (message == null) return;
        sender.send(message);
    }
}

4.编写测试类EmailSendApplicationTests

package com.emailsend;

import com.emailsend.listener.EmailSending;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class EmailSendApplicationTests {


    @Autowired
    private EmailSending emailSending;

    @Test
    void contextLoads() {
        emailSending.sendMailMessage("2482893650@qq.com");
    }

}

📑文章末尾

在这里插入图片描述


http://www.kler.cn/news/149947.html

相关文章:

  • LeetCode无重复字符的最长字符串的Java实现
  • Python武器库开发-前端篇之JavaScript介绍(三十三)
  • linux 内核文件读写
  • oracle常用通用sql脚本——查询前用户的表空间信息
  • 快速操控鼠标行为!Vue鼠标按键修饰符让你事半功倍
  • Qt 自定义标题栏
  • UE 事件分发机制(二) day10
  • lightdb substr函数支持浮点类型
  • 基于Qt QChart和QChartView实现正弦、余弦、正切图表
  • Socket的实现过程
  • 【Python 训练营】N_11 模拟进度条
  • 【Vue】生命周期一文详解
  • WinApp自动化测试之工具的选择
  • java开发需要用到的软件,必备软件工具一览
  • Clickhouse使用总结
  • 搜索与图论算法总结
  • 基于C#实现优先队列
  • Ubuntu上的常用软件配置
  • 人工智能与供应链行业融合:预测算法的通用化与实战化
  • 如何使用ArcGIS Pro制作一张北极俯视地图
  • 初识向量数据库
  • yolov4、yolov5优化策略
  • Vue基本使用(一)
  • [Docker]九.Docker compose讲解
  • AIGC:文本生成视频
  • 【笔记】windows+pytorch:部署一下stable diffusion和NeRF
  • C++ day44完全背包问题 零钱兑换Ⅱ 组合总和Ⅳ
  • C语言基础--#if与#endif
  • 深入了解Spring Boot中@Async注解的8大坑点
  • ISCTF2023 部分wp