扣扣分享交流群:1125844267

一、背景

刚刚在测试别的项目组的项目时,突然发现人家的邮件内容怎么那么好看呢,再看看自己的,就几行字发过去了,不要说客户能不能满意,首先自己就感觉不好看,所以自己也搞一个试试。

二、展示

完成后的邮件内容:

三、代码实现

(1)前端模板(也是自己网上找的,根据自己实际情况做了修改):

<!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="xxxx"><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: #42a3d3; background-color: #49bcff; 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); ">{0}</font></th></tr><tr><td style="word-break:break-all"><div style="padding:25px 35px 40px; background-color:#fff;"><h2 style="margin: 5px 0px; "><font color="#333333" style="line-height: 20px; "><font style="line-height: 22px; " size="4">尊敬的用户:</font></font></h2><!-- 中文 --><p>{1}</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>{2}</p><div style="width:700px;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>xxxxx单位</p><p>办公电话:xxxxx</p><p>电子邮箱:xxxxx</p><p>通讯地址:xxxxx</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>

(2)后端实现
读取邮件模板,替换其中的占位符:
title:邮件内容的标题,如我这儿的“邮箱验证/Email address verification”;
htmlPath:邮件模板地址,如果有多个模板的话可以由传参决定使用哪个,如注册邮箱验证模板地址:/templates/registerEmail.html;
contentCH:中文内容;
contentEN:英文内容;
其实中英文可以是一个参数,但是为了避免在java代码中拼接过多的html代码,所以选择两个参数来做

/*** 读取邮件模板* 替换模板中的信息* @param title* @param htmlPath* @param contentCH* @param contentEN* @return*/public String buildContent(String title, String htmlPath, String contentCH,String contentEN) {//加载邮件html模板Resource resource = new ClassPathResource(htmlPath);InputStream inputStream = null;BufferedReader fileReader = null;StringBuffer buffer = new StringBuffer();String line = "";try {inputStream = resource.getInputStream();fileReader = new BufferedReader(new InputStreamReader(inputStream));while ((line = fileReader.readLine()) != null) {buffer.append(line);}} catch (Exception e) {logger.error("发送邮件读取模板失败",e);} finally {if(fileReader != null){try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}if(inputStream != null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}//替换html模板中的参数String htmlText = MessageFormat.format(buffer.toString(), title, contentCH,contentEN);return htmlText;}

将以上方法返回的字符串当做发送内容发送邮件即可,以下以springboot发送邮件为例进行邮件的发送:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>@Resource
private JavaMailSender javaMailSender;/**
* content:即为以上方法返回的html字符串
*/
public void sendSimpleMail(String to, String subject, String content){MimeMessage message;try {message = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(fromEmail);helper.setTo(to);helper.setSubject(subject);StringBuffer sb = new StringBuffer();sb.append(content);helper.setText(sb.toString(), true);javaMailSender.send(message);} catch (MessagingException e) {e.printStackTrace();}}

利用html模板发送邮件相关推荐

  1. 函数模板案例_利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序 排序规则从大到小,排序算法为选择排序 分别利用char数组和int数组进行测试

    案例描述: 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序 排序规则从大到小,排序算法为选择排序 分别利用char数组和int数组进行测试 #include <iostream& ...

  2. 利用velocity模板以及itext生成pdf

    利用velocity模板以及itext生成pdf 我整理的源码:http://download.csdn.net/download/u012174571/8748897 首先是velocity的使用: ...

  3. 利用C++模板特性计算各整数类型的最大最小值

    基于C++模板编程,我们可以将一部分计算放到编译期来完成,还能够给编写代码带来很多方便. 比如题目中提到的,利用C++模板技术求各整数类型的最大最小值. 代码如下: // indicates T is ...

  4. php模拟登录qq邮箱_PHP 利用QQ邮箱发送邮件的实现

    这篇文章主要介绍了关于PHP 利用QQ邮箱发送邮件的实现,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下在 PHP 应用开发中,往往需要验证用户邮箱.发送消息通知,而使用 PHP 内置的 ...

  5. php smtp邮件类,php利用smtp类发送邮件

    本篇文章主要介绍php利用smtp类发送邮件,感兴趣的朋友参考下,希望对大家有所帮助. 以下就是核心代码:<?php require_once "email.class.php&quo ...

  6. freemarker 生成java_半自动化Java代码生成器[利用freemarker模板生成]

    rapid-generator 半自动化Java代码生成器[利用freemarker模板生成] 增加一些定制和扩展, 修改为基于MAVEN的格式. 整体架构保持不变. 增加的特性为: 支持表名前缀去除 ...

  7. jmail mysql_利用VB+jmail发送邮件源码

    利用VB+jmail发送邮件源码. 一.首先要下载并注册jmail组件,将jmail.dll组件放到C:\Windows\System32(32位)或C:\Windows\SysWOW64(64位)目 ...

  8. 好的设计善于利用PSD模板,轻松搞定促销海报!

    在设计促销海报之前,首先要明白你表达的中心思想是什么?需要突出的主题是什么?那么作图之前首先就要构思好,在这张海报里哪些是需要突出的部分,需要做得够大.够漂亮,这样才能有力传递促销信息. 好的设计善于 ...

  9. java服务器发送邮件_服务器通过模板发送邮件java代码

    //文件读取类 public class TemplateFileRead { private String content= null; public TemplateFileRead() { } ...

  10. esxi中利用ovf模板迁虚拟机

    esxi中利用ovf模板迁虚拟机 在用esxi部署虚拟机后,有时候可能需将上面部署好的虚拟机迁移到其他地方,可以利用导出ovf模板方式进行迁移. 什么是OVF-开放虚拟机格式文件 OVF 是一种虚拟机 ...

最新文章

  1. SLAM精度测评——EVO
  2. 惊艳!基于RNN的AI写词机竟能写出如此优秀的情诗!
  3. PCB设计要点-DDR3布局布线技巧及注意事项
  4. Ardino基础教程 7_蜂鸣器发声实验
  5. win2003主/辅DNS服务器详细配置
  6. php提交多条数据,tpphp一个表单提交多个需要循环的数据怎么处理
  7. matlab guidata两个,Matlab
  8. CF1322C:Instant Noodles
  9. 【渝粤题库】陕西师范大学202012 刑事诉讼法专论 作业
  10. Java FileReader与FileWriter讲解
  11. Rabbitmq消息中间件整合Springboot
  12. 将excel中的数据导入到oracle数据库中
  13. Flink 流批一体在阿里的落地
  14. java学生管理系统论文_(定稿)毕业论文基于java的学生信息管理系统设计报告(完整版)最新版...
  15. 大文件传输的三种方式
  16. 2021年程序员平均工资
  17. Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  18. 真相了!他说:码农和程序员的区别就在这!网友炸锅了
  19. 类似组卷网实现快速组卷功能,实现试题,试卷,课件快速录入、搜索、分类查询,支持mathtype和latex2word。
  20. iOS封装相册API的tips

热门文章

  1. 众觅,让支付宝『到位』全国到位
  2. 科普:什么是IPV4?什么是IPV6?
  3. 网络中的公网和内网 (ipv4)
  4. LR11破解License
  5. javafx设置图片透明度(饱和度,亮度同理)
  6. 怎样设计访谈提纲_如何设计调查问卷与访谈提纲要点分析.ppt
  7. 谷歌浏览器打开特殊端口号地址方法
  8. c盘python怎么移到d盘_怎样把C盘的所有文件移到D盘或E盘里去?
  9. 利用R语言进行主成分分析的步骤
  10. 民国传奇女子——张爱玲