Java Email 发HTML邮件工具 采用 freemarker模板引擎渲染

news2024/9/21 20:50:06

Java Email 发HTML邮件工具 采用 freemarker模板引擎

1.常用方式对比

Java发送邮件有很多的实现方式

  • 第一种:Java 原生发邮件mail.jaractivation.jar

    <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    

    原生的java可以发送普通文本和HTML邮件,同时也可以发送带附件的邮件,但是缺点也很明显,配置非常繁琐,不同的邮件需要不同的实现类去完成,不适合项目中使用。

  • 第二种:使用框架提供的去实现,在SpringBoot中有实现这个功能的组件spring-boot-starter-mail

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

    这种实现起来特别方便,仅仅只要在SpringBoot项目中引入组件,在配置文件中配置好各种参数,就可以实现依赖注入,调用接口完成发送邮件,同时支持普通文本、HTML邮件、以及携带附件的邮件。同时缺点就是和框架集成太高了,如果项目中没有使用SpringBoot就不是那么好用了。

  • 第三种:采用Apache提供的邮件工具commons-email,项目中已经封装好了一些常用的发邮件的接口供开发者使用,并且配置起来也比较简单,和其他技术耦合低是比较好的解决问题的方案。

    <dependency>
    	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-email</artifactId>
    	<version>1.5</version>
    </dependency>
    

2.项目中使用

项目使用的jar包,采用maven工程。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>

第三方jar包引入时下载:https://mvnrepository.com/artifact/org.apache.commons/commons-email

(1)创建配置文件email.properties

  • 建议将配置文件统一放置在resources文件夹中
# 邮件服务器的地址
host=smtp.exmail.qq.com
# 邮件服务器的端口(百度上有,或者邮件提供商开发文档有)
port=456
# 发件人邮箱
username=
# 密码或授权码
password=
# 发件人名称,如果不提供会使用发件人邮件作为发件人
formName=

(2)发邮件的核心代码

package com.zhongcode.demo.email;

import org.apache.commons.mail.*;

import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * 邮件发送实例
 */
public class SendEmail {
    /**
     * 邮件服务器 host
     */
    private String host;
    /**
     * 邮件服务器端口 port
     */
    private int port;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码(授权码)
     */
    private String password;
    /**
     * 发件人名称
     */
    private String formName;
    /**
     * 是否开启debug
     */
    private boolean debug;

    public SendEmail() {
    }

    public SendEmail(Properties pro) {
        this.host = pro.getProperty("host");
        this.port = Integer.parseInt(pro.getProperty("port", "0"));
        this.username = pro.getProperty("username");
        this.password = pro.getProperty("password");
        this.formName = pro.getProperty("formName");
    }

    public SendEmail(String host, int port, String username, String password, String formName, boolean debug) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.formName = formName;
        this.debug = debug;
    }

    public SendEmail(String host, int port, String username, String password, String formName) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.formName = formName;
    }

    public SendEmail(String host, int port, String username, String password) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFormName() {
        return formName;
    }

    public void setFormName(String formName) {
        this.formName = formName;
    }

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    /**
     * 发邮件
     *
     * @param to      收件件人
     * @param subject 主题
     * @param msg     消息
     * @throws EmailException 邮件异常
     */
    public synchronized void sendEmail(String to, String subject, String msg) throws EmailException {
        sendEmail(Collections.singletonList(to), subject, msg, null, null);
    }


    /**
     * 发邮件
     *
     * @param toEmails 发送列表
     * @param subject  主题
     * @param msg      内容支持html
     * @param attaches 附件
     * @param fromName 发送名称
     * @throws EmailException 邮件异常
     */
    public synchronized void sendEmail(List<String> toEmails, String subject, String msg, List<String> attaches, String fromName) throws EmailException {
        // 创建邮件对象
        MultiPartEmail email;
        // 当附件不为空时,使用MultiPartEmail添加附件
        if (attaches != null) {
            email = new MultiPartEmail();
            for (String att : attaches) {
                EmailAttachment attachment = new EmailAttachment();
                attachment.setPath(att);
                // 截取文件名
                attachment.setName(att.substring(att.lastIndexOf("/") + 1));
                email.attach(attachment);
            }
            // 当使用MultiPartEmail时渲染html要使用Part进行添加
            email.addPart(msg, "text/html; charset=UTF-8");
        } else {
            // 默认使用HtmlEmail创建
            email = new HtmlEmail();
            email.setMsg(msg);
        }
        // 设置主机
        email.setHostName(host);
        // 设置端口
        email.setSmtpPort(port);

        email.setDebug(debug);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setCharset("UTF-8");
        email.setSSLOnConnect(true);

        email.setFrom(username, fromName == null ? (this.formName == null ? username : this.formName) : fromName);
        email.setSubject(subject);

        for (String to : toEmails) {
            email.addTo(to);
        }
        email.send();
    }
}

(3)程序发邮件测试

public static void main(String[] args) throws EmailException, IOException, TemplateException {
    // 加载配置文件
    InputStream is = Main.class.getClassLoader().getResourceAsStream("email.properties");
    Properties pro = new Properties();
    pro.load(is);
    // 创建邮件
    SendEmail sendEmail = new SendEmail(pro);
    sendEmail.sendEmail("收件人", "test", "测试邮件");
}

3.发送HTML模板邮件

当我们想发送模板HTML时,需要发送HTML格式的代码,主要是纯文本的邮件并不好看,而且项目中经常使用的是好看的模板,比如系统用户注册时需要发送验证码,很多情况下都要发邮件。

这里我们采用的freemarker模板引擎来渲染我们的HTML模板,下面是以发送邮箱验证码为例。

(1)引入freemarker模板引擎

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.32</version>
</dependency>

(2)创建HTML模板

文件名:code.ftl

<meta charset="utf-8"><table width="100%"><tr><td style="width: 100%;"><center><table class="content-wrap" style="margin: 0px auto; width: 600px;"><tr><td style="margin: 0px auto; overflow: hidden; padding: 0px; border: 0px dotted rgb(238, 238, 238);"><!----><div class="full" tindex="1" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-color: rgb(170, 0, 170); background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 20px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: center; line-height: 1.6; color: rgb(255, 255, 255); font-size: 18px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 1.6; font-size: 18px; margin: 0px;"><strong>系统提示</strong></p></div></div></td></tr></table></td></tr></tbody></table></div><div class="full" tindex="2" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 20px 20px 5px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: left; line-height: 20px; color: rgb(102, 102, 102); font-size: 14px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 20px; font-size: 14px; margin: 0px;">您的验证码为:</p></div></div></td></tr></table></td></tr></tbody></table></div><div class="full" tindex="3" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 20px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: center; line-height: 20px; color: rgb(0, 0, 0); font-size: 24px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 20px; font-size: 24px; margin: 0px;"><strong>${code}</strong></p></div></div></td></tr></table></td></tr></tbody></table></div><div class="full" tindex="4" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 8px 20px 20px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: left; line-height: 20px; color: rgb(102, 102, 102); font-size: 12px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 20px; font-size: 12px; margin: 0px;"><span style="color: #333333;">验证码10分钟内有效。</span></p></div></div></td></tr></table></td></tr></tbody></table></div><div class="full" tindex="5" style="margin: 0px auto; line-height: 0px; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td align="center" class="fullTd" style="direction: ltr; font-size: 0px; padding: 10px 20px; text-align: center; vertical-align: top; word-break: break-word; width: 600px; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-spacing: 0px;"><tbody><tr><td style="width: 600px; border-top: 1px solid rgb(204, 204, 204);"></td></tr></tbody></table></td></tr></tbody></table></div><div class="full" tindex="6" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 10px 20px 0px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: left; line-height: 20px; color: rgb(170, 170, 170); font-size: 12px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 20px; font-size: 12px; margin: 0px;">此为系统邮件请勿回复</p></div></div></td></tr></table></td></tr></tbody></table></div><div class="full" tindex="7" style="margin: 0px auto; max-width: 600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" class="fullTable" style="width: 600px;"><tbody><tr><td class="fullTd" style="direction: ltr; width: 600px; font-size: 0px; padding-bottom: 0px; text-align: center; vertical-align: top; background-image: url(&quot;&quot;); background-repeat: no-repeat; background-size: 100px; background-position: 10% 50%;"><table border="0" cellpadding="0" cellspacing="0" width="100%" style="vertical-align: top;"><tr><td align="left" style="font-size: 0px; padding: 20px;"><div class="text" style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;; overflow-wrap: break-word; margin: 0px; text-align: center; line-height: 20px; color: rgb(170, 170, 170); font-size: 12px; font-weight: normal;"><div><p style="text-size-adjust: none; word-break: break-word; line-height: 20px; font-size: 12px; margin: 0px;">Copyright © zhongcode 2020 All Right Reserved</p></div></div></td></tr></table></td></tr></tbody></table></div></td></tr></table></center></td></tr></table>

(3)代码测试

public class Main {
    public static void main(String[] args) throws EmailException, IOException, TemplateException {

        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("E:\\tmp\\ftl"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        //模板名自动在模板所在目录寻找
        Template template = configuration.getTemplate("code.ftl");
        Map<String, String> data = new HashMap<>();
        data.put("code", "999999");
        StringWriter out = new StringWriter();
        template.process(data, out);
        // 加载配置文件
        InputStream is = Main.class.getClassLoader().getResourceAsStream("email.properties");
        Properties pro = new Properties();
        pro.load(is);
        // 创建邮件
        SendEmail sendEmail = new SendEmail(pro);
        sendEmail.sendEmail("收件人", "验证码", out.toString());
    }

}

在这里插入图片描述

就这样,一封好看的HTML邮件就完成了,任何问题都可以私信我哦

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/343355.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

使用VUE自定义组件封装部门选择功能

背景 照惯例&#xff0c;先交待下背景&#xff0c;从真实需求出发&#xff0c;讲述实现效果、设计思路和实现方式。 软件系统中&#xff0c;会有一些常见常用的选择功能&#xff0c;如部门选择、人员选择等&#xff0c;用于填报表单&#xff0c;使用频率很高。直接使用一方面会…

浅谈应用安全测试工具

正确的应用程序安全测试工具可以改善企业安全态势和开发工作流程。如今&#xff0c;应用程序安全从一开始就内置在整个软件生命周期中&#xff0c;即使是具有成熟开发实践的组织也需要自动化工具来在复杂、快速变化的环境中成功地保护他们的软件。以下比较了三个广泛使用的应用…

MAVEN打包这一篇就够了

Maven打包说明IDEA目录结构Java代码从编码到最后运行到两个核心步骤为"编译"和"执行"。"编译"会根据"源代码"产出对应的".class"文件&#xff0c;而"执行"就是让程序运行起来&#xff0c;运行的对象就是这些"…

【C语言学习笔记】:动态库

一、动态库 通过之前静态库那篇文章的介绍。发现静态库更容易使用和理解&#xff0c;也达到了代码复用的目的&#xff0c;那为什么还需要动态库呢&#xff1f; 1、为什么还需要动态库&#xff1f; 为什么需要动态库&#xff0c;其实也是静态库的特点导致。 ▶ 空间浪费是静…

怎么打造WhatsApp Team?SaleSmartly(ss客服)告诉你

关键词&#xff1a;WhatsApp Team SaleSmartly&#xff08;ss客服&#xff09; 您是否正在寻找一种让您的团队能够在 WhatsApp协作消息传递的解决方案?拥有了 WhatsApp Team&#xff0c;不仅效率提升&#xff0c;还可以在智能聊天工具中比如SaleSmartly&#xff08;ss客服&…

51单片机——中断系统,小白讲解,相互学习

中断介绍 中断是为使单片机具有对外部或内部随机发生的事件实时处理而设置的&#xff0c;中断功能的存在&#xff0c;很大程度上提高了单片机处理外部或内部事件的能力。它也是单片机最重要的功能之一&#xff0c;是我们学些单片机必须要掌握的。 为了更容易的理解中断概念&…

算法思想 - 动态规划算法

动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中&#xff0c;可能会有许多可行解。每一个解都对应于一个值&#xff0c;我们希望找到具有最优值的解。动态规划算法与分治法类似&#xff0c;其基本思想也是将待求解问题分解成若干个子问题&#xff0c;先求解子问…

哈希表题目:判断路径是否相交

文章目录题目标题和出处难度题目描述要求示例数据范围解法一思路和算法代码复杂度分析解法二思路和算法代码复杂度分析题目 标题和出处 标题&#xff1a;判断路径是否相交 出处&#xff1a;1496. 判断路径是否相交 难度 3 级 题目描述 要求 给你一个字符串 path\texttt…

【软考系统架构设计师】2022下案例分析历年真题

【软考系统架构设计师】2022下案例分析历年真题 【软考系统架构设计师】2022下案例分析历年真题【软考系统架构设计师】2022下案例分析历年真题2022下案例分析历年真题第一题&#xff08;25分&#xff09;2022下案例分析历年真题第二题&#xff08;25分&#xff09;2022下案例分…

使用纹理(Textures)

当物体表面并非是纯色的时候&#xff0c;比如带波点&#xff0c;斑纹或者表面有刮痕或被裂纹等&#xff0c;这些效果该如何实现呢&#xff1f; 这里我们需要提到一个概念是贴图&#xff08;Maps&#xff09;。Maps是覆盖在游戏物体上的2D图片&#xff0c;用来设置表面的颜色、s…

大数据-学习实践-1相关Linux

大数据-学习实践-1相关Linux (大数据系列) 文章目录大数据-学习实践-1相关Linux1知识点2具体内容2.1安装、使用2.2高级命令2.2.1文件2.2.2日期2.2.3进程2.2.4三剑客 (grep、sed、awk)2.3高级配置2.3.1分配IP&#xff1a;静态IP设置2.3.2起名&#xff08;hostname&#xff09;&…

工程经验:残差连接对网络训练的巨大影响

文章目录1、没有使用残差连接的网络难以训练2、loss 不下降的原因3、使用了残差连接的网络可以高效训练1、没有使用残差连接的网络难以训练 经典的 SegNet 网络结构如下&#xff1a; 在使用上图所示的 SegNet 作为噪声预测网络训练扩散模型&#xff08;DDPM&#xff09;时&…

Elasticsearch汉字补全和智能纠错使用详解

1 使用ES实现的效果 汉字补全 拼写纠错

Python自动化测试【软件测试最全教程(附笔记、学习路线)】,看完即就业

最近看到很多粉丝在后台私信我&#xff0c;叫我做一期Python自动化测试的教程&#xff0c;其实关于这个问题&#xff0c;我也早就在着手准备了&#xff0c;我录制了一整套完整的Python自动化测试的教程&#xff0c;上传到网盘里了&#xff0c;大家有兴趣的可以去文末交流群免费…

[架构之路-107]-《软考-系统架构设计师》-0-系统分析师与系统架构设计师简介与官网介绍

官网链接&#xff1a;https://www.ruankao.org.cn/index/ind计算机技术与软件专业技术资格&#xff08;水平&#xff09;考试简介计算机技术与软件专业技术资格&#xff08;水平&#xff09;考试&#xff08;以下简称计算机软件资格考试&#xff09;是原中国计算机软件专业技术…

化学试剂Glutaric Acid-PEG-Glutaric Acid,GA-PEG-GA,戊二酸-聚乙二醇-戊二酸

一&#xff1a;产品描述 1、名称 英文&#xff1a;Glutaric Acid-PEG-Glutaric Acid&#xff0c;GA-PEG-GA 中文&#xff1a;戊二酸-聚乙二醇-戊二酸 2、CAS编号&#xff1a;N/A 3、所属分类&#xff1a;Carboxylic acid PEG 4、分子量&#xff1a;可定制&#xff0c; 戊…

如果网站的 Cookie 特别多特别大,会发生什么(一)

有没有想过&#xff0c;如果网站的 Cookie 特别多特别大&#xff0c;会发生什么情况&#xff1f; 不多说&#xff0c;马上来试验一下&#xff1a; for (i 0; i < 20; i) document.cookie i X.repeat(2000) 什么&#xff0c;网站居然报错了&#xff1f; 众所周知&am…

【Unity VR开发】结合VRTK4.0:自身移动(滑动)

语录&#xff1a; 依山傍水房树间&#xff0c;行也安然&#xff0c;住也安然&#xff1b; 一条耕牛半顷田&#xff0c;收也凭天&#xff0c;荒也凭天&#xff1b; 雨过天晴驾小船&#xff0c;鱼在一边&#xff0c;酒在一边&#xff1b; 夜晚妻子话灯前&#xff0c;今也谈谈…

考研复试机试 | C++

目录1.盛水最多的容器<11>题目代码&#xff1a;2.整数转罗马数字题目&#xff1a;代码&#xff1a;3. 清华大学机试题 abc题目题解4.清华大学机试题 反序数题目描述代码对称平方数题目代码&#xff1a;5. 杭电上机题 叠筐题目&#xff1a;代码pass&#xff1a;关于清华大…

Windows server——部署DNS服务(3)

作者简介&#xff1a;一名云计算网络运维人员、每天分享网络与运维的技术与干货。 座右铭&#xff1a;低头赶路&#xff0c;敬事如仪 个人主页&#xff1a;网络豆的主页​​​​​​ 目录 前言 一.管理DNS服务 1.子域 案例 2. 委派 案例 1&#xff09;添加主机记录 …