首页 > 其他分享 >SpringBoot发送邮件

SpringBoot发送邮件

时间:2024-06-23 15:22:58浏览次数:3  
标签:SpringBoot 发送 context org import setVariable 邮件

SpringBoot集成邮件服务

本文将介绍如何使用Spring Boot集成QQ邮箱的邮件服务,实现Java发送邮件功能。

开启邮件服务

以QQ邮箱为例,首先需要开启SMTP服务。

image-20240623135859038 image-20240623140042657
第一步 首页点击设置 第二步 点击账号
image-20240623140203855 image-20240623140522737
第三步 下滑找到SMTP相关文件服务,获取验证码 第四步 复制获取到的授权码

引入依赖

pom.xml文件中引入发送邮件和Thymeleaf模板引擎相关依赖。

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

配置邮件服务

application.yml文件中添加邮件服务的配置。

spring:
  mail:
    host: smtp.qq.com
    username: [email protected] 
    password: XXXXXXXXXXXXXXX(授权码)
    port: 587
    default-encoding: UTF-8

实现邮件服务类

创建一个服务类用于发送邮件。

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;

@RestController
public class EmailController {

    // 自动注入EmailService
    @Autowired
    private MailService mailService;

    /**
     * 发送邮件的端点
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @return 发送结果
     */
    @GetMapping("/send-email")
    public String sendEmail(@RequestParam String to, @RequestParam String subject) {
        Context context = new Context();
        context.setVariable("name", "张三");
        context.setVariable("event", "日常聚会");
        context.setVariable("location", "欢乐广场");
        context.setVariable("date", "2024-06-23");
        context.setVariable("time", "19:30");
        context.setVariable("image","background");
        String message = null;
        try {
            mailService.sendSimpleMail(to,subject,"invitationTemplate",context);
            message = "邮件发送成功";
        }catch (MessagingException e) {
            message = "邮件发送失败";
        }
        return message;
    }
}

创建邮件模板

src/main/resources/templates目录下新建文件invitationTemplate.html

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>邮件模板</title>
</head>
<body>
<h1>亲爱的<span th:text="${name}"></span>,</h1>
<p>我们诚挚地邀请您参加我们的活动 <strong><span th:text="${event}"></span></strong>。</p>
<p>活动地点:<span th:text="${location}"></span></p>
<p>活动日期:<span th:text="${date}"></span></p>
<p>活动时间:<span th:text="${time}"></span></p>
<img th:src="'cid:' + ${image}" alt="活动图片"/>
<p>期待您的光临!</p>
</body>
</html>

创建邮件发送控制器

创建一个控制器来处理发送邮件的请求。

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;

@RestController
public class EmailController {

    // 自动注入EmailService
    @Autowired
    private MailService mailService;

    /**
     * 发送邮件的端点
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @return 发送结果
     */
    @GetMapping("/send-email")
    public String sendEmail(@RequestParam String to, @RequestParam String subject) {
        Context context = new Context();
        context.setVariable("name", "张三");
        context.setVariable("event", "日常聚会");
        context.setVariable("location", "欢乐广场");
        context.setVariable("date", "2024-06-23");
        context.setVariable("time", "19:30");
        context.setVariable("image","background");
        String message = null;
        try {
            mailService.sendSimpleMail(to,subject,"invitationTemplate",context);
            message = "邮件发送成功";
        }catch (MessagingException e) {
            message = "邮件发送失败";
        }
        return message;
    }
}

邮件发送测试

通过 postman 进行测试。

image-20240623141503544

去邮箱查看是否收到邮件。

image-20240623141601251

标签:SpringBoot,发送,context,org,import,setVariable,邮件
From: https://www.cnblogs.com/lidy5436/p/18263478

相关文章

  • SpringBoot使用spring.factories加载默认配置
    在日常开发过程中,发布一些产品或者框架时,会遇到某些功能需要一些配置才能正常运行,这时我们需要的提供默认配置项,同时用户也能覆盖进行个性化创建InitializerpublicclassFrameContextInitializerimplementsApplicationContextInitializer{@Overridepublicvoid......
  • 计算机毕业设计|基于微信小程序的宠物自动喂养系统 基于SpringBoot的宠物自动喂养系统
    ......
  • SpringBoot + 虚拟线程,鸟枪换大炮!
    “虚拟”线程,望文生义,它是“假”的,它不直接调度操作系统的线程,而是由JVM再提供一层线程的接口抽象,由普通线程调度,即一个普通的操作系统线程可以调度成千上万个虚拟线程。虚拟线程比普通线程的消耗要小得多得多,在内存足够的情况下,我们甚至可以创建上百万的虚拟线程,这在之前(Jav......
  • SpringBoot前后端传递数据时常用的JSON格式数据是什么?【讲解JSON概念、语法、以及Java
    SpringBoot前后端传递数据时常用的JSON格式数据是什么?JSON概念JSON语法JSON的两种结构:JSON字符串和Java对象互转:objectMapper.writeValueAsString(person);objectMapper.readValue(jsonStr,Person.class);在SpringMVC框架中,前后端交互会自动转JsonJSON概念JSON:Jav......
  • 学生读书笔记共享系统-毕业设计-Springboot+mysql+Vue
    介绍学生读书笔记共享系统是一款专为学生设计的平台,旨在通过信息化手段实现读书笔记的共享和交流。系统分为管理端和用户端两个角色,分别为管理员和学生用户提供不同的功能模块,满足各自的需求。该系统不仅促进了学生之间的学习交流,还提升了学习效率和笔记管理的便捷性。技术栈......
  • 基于springboot的技术交流和分享平台 毕业设计 springboot+VUE
    介绍在当今迅速发展的信息时代,技术交流和知识分享已成为推动创新和个人成长的重要途径。然而,许多现有平台在笔记管理和分类上存在不足,缺乏有效的知识组织和分享机制,导致信息获取效率低下,交流互动有限。为了解决这些问题,我开发了一款基于SpringBoot的技术交流和分享平台。该平......
  • Springboot计算机毕业设计自动答疑系统小程序【附源码】开题+论文+mysql+程序+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着教育信息化的快速发展,学生对于知识获取和问题解决的需求日益增加。然而,传统的答疑方式,如面对面咨询或邮件回复,存在效率低下、资源分配不均等问题......
  • 基于springboot的信息技术知识竞赛系统源码数据库
    传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装信息技术知识赛系统软件来发挥其高效地信息处理的作用,可以规范信息管理流程,让管理工作可以系统化和程序化,同时,信息技术知识赛系统的......
  • 基于springboot的贸易行业crm系统源码数据库
    随着信息技术在管理上越来越深入而广泛的应用,管理信息系统的实施在技术上已逐步成熟。本文介绍了基于springboot的贸易行业crm系统的开发全过程。通过分析基于springboot的贸易行业crm系统管理的不足,创建了一个计算机管理基于springboot的贸易行业crm系统的方案。文章介绍了基......
  • springboot+手机商城网站-计算机毕业设计源码201029
    摘 要在信息飞速发展的今天,网络已成为人们重要的信息交流平台。手机店每天都有大量的手机商品需要通过网络发布,为此,本人开发了一个基于springboot手机商城网站。本系统采用跨平台的JAVA语言开发,利用springboot框架进行逻辑控制,MySQL数据库存储数据,最后Tomcat服务器完成发布......