Spring boot 发送文本邮件 和 html模板邮件


提示:以下是本篇文章正文内容,下面案例可供参考

一、开启QQ邮箱里的POP3/SMTP服务

①:开启步骤

1.邮箱设置 -->账号

在这里插入图片描述

2.开启服务(并复制 授权码

在这里插入图片描述在这里插入图片描述

二、简单配置

①:引入依赖

  <!-- 邮件发送-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
  </dependency>

②:yml配置

  mail:
    username: xxxx@foxmail.com
    # QQ邮箱应该使用授权码
    password: \*\*\*\*\*ek\*\*\*
    ## 邮箱服务器地址 smtp.qq.com
    host: smtp.qq.com
    #使用SMTPS协议465端口
    port: 465
    # ssl 配置
    properties:
      mail.smtp.starttls.required: true
      encoding: UTF-8
      mail.smtp.ssl.enable: true
      mail.smtp.auth: true
      mail.smtp.socketFactory.port: 465
      mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
      mail.smtp.socketFactory.fallback: false

三、发送纯文本文件

①:写个工具类

@Component
@Slf4j
public class UtilsSendCode {
    @Value("${spring.mail.username}")
    private String sender;

    @Resource
    private JavaMailSenderImpl mailSender;

    /\*\*
 \* 发送纯文字邮件
 \* @param to 收件人
 \* @param subject 主题
 \* @param content 内容
 \*/
    public void sendSimpleMail(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(sender);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        try {
            mailSender.send(message);
            log.info("邮件接收人"+to+"主题"+subject+"内容"+content+"邮件发送成功");
        }catch (Exception e){
            log.error("邮件接收人"+to+"主题"+subject+"内容"+content+"邮件发送出现异常");
            log.error("异常信息为"+e.getMessage());
        }
    }
}

②:在业务层调用方法发送即可

  // 开始发送邮件
  utilsSendCode.sendSimpleMail("xxxxxx@gmail.com", "验证码", "123456");

在这里插入图片描述

③:发送成功

1.发送测试

在这里插入图片描述

2.收到验证码

在这里插入图片描述

四、发送Html模板邮件

①:定义一个Html模板

1.template/mailTemplate.ftl

在这里插入图片描述

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="email code">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<!--邮箱验证码模板-->
<body>
<div style="background-color:#ECECEC; padding: 35px;">
    <table cellpadding="0" align="center"
 style="width: 800px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;">
        <tbody>
        <tr>
            <th valign="middle"
 style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">
                <font face="微软雅黑" size="5" style="color: rgb(255, 255, 255); ">这里输入name</font>
            </th>
        </tr>
        <tr>
            <td style="word-break:break-all">
                <div style="padding:25px 35px 40px; background-color:#fff;opacity:0.8;">

                    <h2 style="margin: 5px 0px; ">
                        <font color="#333333" style="line-height: 20px; ">
                            <font style="line-height: 22px; " size="4">
                                尊敬的用户:</font>
                        </font>
                    </h2>
                    <!-- 中文 -->
                    <p>您好!感谢您使用****,您的账号正在进行邮箱验证,验证码为:<font color="#ff8c00">{0}</font>,有效期30分钟,请尽快填写验证码完成验证!</p><br>
                    <!-- 英文 -->
                    <h2 style="margin: 5px 0px; ">
                        <font color="#333333" style="line-height: 20px; ">
                            <font style="line-height: 22px; " size="4">
                                Dear user:</font>
                        </font>
                    </h2>
                    <p>Hello! Thanks for using *****, your account is being authenticated by email, the
                        verification code is:<font color="#ff8c00">{0}</font>, valid for 30 minutes. Please fill in the verification code as soon as
                        possible!</p>
                    <div style="width:100%;margin:0 auto;">
                        <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
                            <p>****团队</p>
                            <p>联系我们:********</p>
                            <br>
                            <p>此为系统邮件,请勿回复<br>
                                Please do not reply to this system email
                            </p>
                            <!--<p>©\*\*\*</p>-->
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>



②:在工具类中新增两个方法

  • 根据模板生成邮件内容
    /\*\*
 \* 根据模板生成邮件内容
 \* @param code 验证码
 \* @return
 \*/
 **自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/8657cac74b42cc6f7b469dd423562747.png)

![img](https://img-blog.csdnimg.cn/img_convert/20f7ffe7aa0ae9d05dd578284da0b651.png)

![img](https://img-blog.csdnimg.cn/img_convert/eab37bdcc612807ed018c8534330b3f2.png)

![img](https://img-blog.csdnimg.cn/img_convert/da66e246e04fe7003f463303eb4d3665.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

5e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

<img src="https://img-community.csdnimg.cn/images/fd6ebf0d450a4dbea7428752dc7ffd34.jpg" alt="img" style="zoom:50%;" />
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合邮件发送并保存历史发送邮箱 项目描述 项目主要是使用 Spring Boot 发送邮件,主要的技术点有: 1、Spring Boot +mybatis的整合 2、Spring Boot项目中jsp的使用 3、Spring Boot 发送邮件文本格式的邮件发送HTML格式的邮件发送带附件 的邮件发送带静态资源的邮件) 个人觉得Springboot的开发简单的归纳为三步jar包引入,配置,应用。 (一)简单使用 1)JSP的使用配置 Spring Boot整合邮件发送并保存历史发送邮箱 Spring Boot整合邮件发送并保存历史发送邮箱 2) 邮件发送服务 1、pom 包配置 pom.xml 引入加 spring-boot-starter-mail 依赖包: Spring Boot整合邮件发送并保存历史发送邮箱 2、配置文件 application.yml Spring Boot整合邮件发送并保存历史发送邮箱 注意:测试时需要将 spring.mail.username 和 spring.mail.password 改成自己邮箱对应的登录名和密码,这里的密码不是邮箱的登录密码,是开启 POP3 之后设置的客户端授权密码。 MailServiceImpl.java JavaMailSender (1)Spirng 已经帮我们内置了 JavaMailSender,直接在项目中引用即可。我们封装一个 MailService 类来实现普通的邮件发送方法。 Spring Boot整合邮件发送并保存历史发送邮箱 from,即为邮件发送者; to,邮件接收者; subject,邮件主题; content,邮件的主体。 邮件发送者 from 一般采用固定的形式写到配置文件中。 (2)富文本邮件 在日常使用的过程中,通常在邮件中加入图片或者附件来丰富邮件的内容 发送 HTML 格式邮件 邮件发送支持以 HTML 的形式去构建我们喜欢的文本格式,SpringHTML 格式的邮件也做出了支持,非常方便使用。 我们在 MailService 中添加支持 HTML 发送的方法. Spring Boot整合邮件发送并保存历史发送邮箱 和上面对比,这次发送邮件使用 MimeMessageHelper 类。MimeMessageHelper 支持发送复杂邮件模板,支持文本、附件、HTML、图片等,接下来我们会继续使用。 (3)发送带附件的邮件 在 MailService 添加 sendAttachmentsMail 方法。 Spring Boot整合邮件发送并保存历史发送邮箱 (4)发送带静态资源的邮件 邮件中的静态资源一般就是指图片,在 MailService 添加 sendAttachmentsMail 方法。 Spring Boot整合邮件发送并保存历史发送邮箱 相关测试在这里就省略了 (二)本项目中主要以发送 HTML 格式邮件为例,发送邮件并把邮箱保存到数据库中 FreeMarker模板引擎 Spring Boot整合邮件发送并保存历史发送邮箱 邮件模板 Spring Boot整合邮件发送并保存历史发送邮箱 运行环境 jdk8+tomcat8+mysql+IntelliJ IDEA+maven 项目技术(必填) springboot +mybatis +jquery+jsp 数据库文件 压缩包内 jar包文件 maven搭建
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值