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

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.compassword: 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 {@AutowiredJavaMailSender 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 {@Autowiredprivate EmailSending emailSending;@Testvoid contextLoads() {emailSending.sendMailMessage("2482893650@qq.com");}}

📑文章末尾

在这里插入图片描述

http://www.lryc.cn/news/248125.html

相关文章:

  • Mac单独修改应用语言
  • Unity 通过代码控制Texture进行缩放
  • C语言:输入3个整数,按由小到大的顺序输出(指针)
  • C# 模拟鼠标操作工具类
  • content_script.js、background.js和popup.js之间的通讯
  • python的requests请求参数带files
  • Elk:filebeat 日志收集工具和logstash
  • [设计模式] 常见的设计模式
  • 报错解决:You may need an additional loader to handle the result of these loaders.
  • 配置自动化部署Jenkins和Gitea
  • VSCODE+QEMU+WSL调试RISCV代码(SBI、kernel)
  • 二叉树(判断是否为对称二叉树)
  • STM32开发学习(地址映射)
  • 证明E(X+Y) =E(X) + E(Y)
  • ClickHouse入门手册1.0
  • 10个火爆的设计素材网站推荐
  • SQL注入 - CTF常见题型
  • android keylayout键值适配
  • python读取excel自动化生成sql建表语句和java实体类字段
  • Unity求向量A在平面L上的投影向量
  • 人机交互2——任务型多轮对话的控制和生成
  • 【数据结构】八大排序 (三)
  • Redis 命令处理过程
  • python爬虫进阶教程之如何正确的使用cookie
  • 【hacker送书第4期】推荐4本Java必读书籍(各送一本)
  • [密码学]DES
  • 15个超级实用的Python操作,肯定有你意想不到的!
  • GitHub上8个强烈推荐的 Python 项目
  • 什么是依赖倒置原则
  • 异常数据检测 | Python实现oneclassSVM模型异常数据检测