前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot使用FreeMarker模板发送邮件

SpringBoot使用FreeMarker模板发送邮件

作者头像
游离于山间之上的Java爱好者
发布2022-12-19 11:00:17
1.3K0
发布2022-12-19 11:00:17
举报
文章被收录于专栏:你我杂志刊你我杂志刊

由于本文属于付费文章,获取全文,可点击文末【阅读原文】,即可到达【秋码记录】https://qiucode.cn/article/96

1、添加相关依赖

1.1、使用Maven,在pom.xml添加如下依赖:

代码语言:javascript
复制
<!-- 邮件所需依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency> 

<!-- Spring Boot Freemarker 依赖,发送HTML格式的邮件的方式 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>        
</dependency>

1.2、使用Gradle,则在 build.gradle 添加依赖:

代码语言:javascript
复制
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-freemarker'
  implementation 'org.springframework.boot:spring-boot-starter-mail'
  //... 其他依赖库
}

2、application.yml 配置

代码语言:javascript
复制
#其他配置信息
spring:
    #其他配置信息
    freemarker:
        suffix: .ftl
        template-loader-path:
          - classpath:/templates
        cache: false
        charset: UTF-8
        content-type: text/html
    mail:
        host: smtp.163.com
        username: #添加您自己163邮箱  
        password: #授权第三方登录的授权码  不是邮箱密码哦  别弄混了
        protocol: smtp
        properties.mail.smtp.auth: true
        properties.mail.smtp.port: 994 #465或者994
        #properties.mail.display.sendmail: Javen
        #properties.mail.display.sendname: Spring Boot Guide Email
        properties.mail.smtp.starttls.enable: true
        properties.mail.smtp.starttls.required: true
        properties.mail.smtp.ssl.enable: true
        default-encoding: utf-8
        from: #添加您自己163邮箱

3、编写MailService服务

代码语言:javascript
复制
package cn.qiucode.blog.service;
import cn.qiucode.blog.entity.Message;
/**
 * @program: qiucode-blog
 * @description: 发送邮箱sevice类
 * @author: 上官江北
 * @create: 2021-04-17 20:02
 */
public interface MailService  {
    /**
     * 使用模板发送邮件
     * @param message       评论或留言对象
     * @param title         邮件标题
     * @param templateName  模板名称
     */
    public void sendMessageMail(Message message, String title, String templateName);
}

最终实现效果如图:

由于本篇文章属于付费才可阅读,获取全文阅读,可点击文末的【阅读原文】

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 你我杂志刊 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 由于本文属于付费文章,获取全文,可点击文末【阅读原文】,即可到达【秋码记录】https://qiucode.cn/article/96
  • 1、添加相关依赖
  • 2、application.yml 配置
  • 3、编写MailService服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档