使用javaMail和velocity来发送模板邮件
之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示。
这类邮件主要用于公司的推广宣传,比如商城的促销等场景。
今天打算将邮件模块也集成到ssm项目,也算是对之前做的东西的一种巩固。
简单邮件模块
首先来集成简单的邮件模块。
1.第一步,加jar包,在maven的pom.xml加入如下代码。
- <!-- 邮件 -->
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>javax.mail-api</artifactId>
- <version>1.6.0</version>
- </dependency>
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>smtp</artifactId>
- <version>1.6.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>mailapi</artifactId>
- <version>1.6.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
2.编写mail.properties配置文件
- #邮箱参数配置
- mail.host:smtp.qq.com
- mail.port:25
- mail.username:xxxxx
- mail.password:xxxxxx
3.在spring主配置文件中引入此配置文件
- <!-- 读取properties文件 参数配置,所有的参数配置均可放在此处 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- <value>classpath:mail.properties</value>
- </list>
- </property>
- </bean>
4.新建spring-mail.xml文件,用于配置邮件相关的内容。
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
- <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="${mail.host}" />
- <property name="port" value="${mail.port}" />
- <property name="username" value="${mail.username}" />
- <property name="password" value="${mail.password}" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
- </beans>
5.在主配置文件中引入spring-mail.xml配置文件。
- <import resource="spring-mail.xml" />
6.在一个controller类中编码调用邮件,注意需要注入mailSender这个bean.
- @Resource(name = "mailSender")
- private JavaMailSender mailSender;
- @ResponseBody
- @RequestMapping(value = "simpleMailDemo", produces = "text/html; charset=utf-8")
- public String simpleMailDemo() {
- SimpleMailMessage mail = new SimpleMailMessage();
- JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
- // 发送方
- mail.setFrom(mailSenderImpl.getUsername());
- // 接收方
- mail.setTo("2717814812@qq.com");
- // 标题
- mail.setSubject("spring mvc简单邮件发送");
- // 设置邮件正文
- mail.setText("spring mvc简单邮件发送,收到此邮件表明邮件模块配置成功");
- mailSender.send(mail);
- return "成功";
- }
7.在浏览器输入http://192.168.1.185:8080/warrior/simpleMailDemo,结果如图。
看看邮箱,确实收到了邮件。
采用velocity模板编写富文本邮件
1.第一步,引入velocity所需要用到的jar文件。
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity</artifactId>
- <version>1.7</version>
- </dependency>
2.在spring-mail.xml中加入关于velocity的配置。
- <bean id="velocityEngine"
- class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
- <property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!--
- 模板存放的路径 -->
- <property name="configLocation" value="classpath:velocity.properties" />
- </bean>
- <!--配置试图解析器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <property name="cache" value="false" />
- <property name="prefix" value="" />
- <property name="suffix" value=".vm" />
- <property name="contentType" value="text/html;charset=utf-8" />
- <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" />
- <property name="exposeSpringMacroHelpers" value="true" />
- <property name="exposeRequestAttributes" value="true" />
- <property name="exposeSessionAttributes" value="true" />
- <property name="allowSessionOverride" value="true" />
- <property name="allowRequestOverride" value="true" />
- </bean>
注意上述配置我们将velocity的模板存在了web-inf下的templates文件夹下,并且启用了toolbox.xml配置,toolbox.xml主要是一些工具,你可以把你在编写velocity模板的时候可能会用到的工具填写到toolbox.xml中。
3.注意上述配置文件引入了velocity.properties文件,在resources下新建velocity.properties文件。
- input.encoding=UTF-8
- output.encoding=UTF-8
4.在web-inf下新建templates文件夹,新建toolbox.xml,此处我随便写了两个工具到toolbox.xml里面。
- <?xml version="1.0" encoding="UTF-8" ?>
- <toolbox>
- <tool>
- <key>UrlUtil</key>
- <class>com.nali.common.util.UrlUtil</class>
- <scope>application</scope>
- </tool>
- <tool>
- <key>VersionUtil</key>
- <class>com.ximalaya.shop.api.domain.util.VersionUtil</class>
- <scope>application</scope>
- </tool>
- </toolbox>
4.编写velocity模板,在templates文件夹下面新建一个名为test.vm的模板。
- <html>
- <table style="display:none"><tr><td>逸品商城,优质农产品直供平台,会员专享,按需定制,个性化定产品,大宗商品,会员卡,宅配卡,逸品资讯,营养师服务,逸品云创亲子体验馆|WONYEN.COM</td></tr></table>
- <table align="center" cellpadding="0" cellspacing="0" style="width:720px;border:0px;background-color:White;margin:0 auto;padding:10px 0px 0px 0px;">
- <tbody>
- <tr>
- <td style="width:720px;height:40px;">
- <table style="width:720px;height:40px;">
- <tbody>
- <tr>
- <td style="width:130px;vertical-align:top;">
- <a href="http://www.wonyen.com" target="_blank"><img src="http://wonyen.com/Images/logo_mail.png" border="0" width="160" height="40"></a>
- </td>
- <td style="width:230px;"></td>
- <td style="width:90px;text-align:center;">
- <a href="http://wonyen.com/" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">首页</a>
- </td>
- <td style="width:90px;text-align:center;">
- <a href="http://wonyen.com/yipinNews_All" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">逸品资讯®</a>
- </td>
- <td style="width:90px;text-align:center;">
- <a href="http://bbs.wonyen.com/" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">会员社区 </a>
- </td>
- <td style="width:90px;text-align:center;">
- <a href="http://wonyen.com/yipinStreet" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">逸品街 </a>
- </td>
- </tr>
- </tbody>
- </table>
- </td>
- </tr>
- <tr style="height: 15px;"></tr>
- <tr>
- <td style="height: 40px;color: #282828;font-size: 16pt;font-weight: 700;">
- 逸品<span style="color: #f6531a;">资讯®</span>
- </td>
- </tr>
- <!--产品列表-->
- #foreach($product in $productList)
- <tr>
- <td style="width:720px;height:100px;padding:20px 0px 15px 0px;border-bottom-color: #ccc;border-bottom-width: 1px;border-bottom-style: dotted;">
- <table style="width:720px;">
- <tbody>
- <tr>
- <td rowspan="2" style="width:138.9px;">
- <a href="http://www.qgranite.com/productDetail?productId=$product.productId" target="_blank"><img style="border: 1px solid #ddd;" src="http://www.qgranite.com/$product.productPic" border="0" width="138.9" height="100"></a>
- </td>
- <td style="width:570px;height:20px;padding-left:10px;">
- <a href="http://www.qgranite.com/productDetail?productId=$product.productId" target="_blank" style="font-weight:700;text-decoration:none;color:black;font-size:13pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">$product.productName</a>
- </td>
- </tr>
- <tr>
- <td style="width:570px;height:80px;padding-left:10px;line-height:25px;font-size:11pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;color:#333333;">$product.description</td>
- </tr>
- </tbody>
- </table>
- </td>
- </tr>
- #end
- <tr>
- <td>
- <table>
- <tbody>
- <tr>
- <td style="width:360px;height:20px;text-align:center;padding:0px 0px 0px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">
- <table style="width:360px;">
- <tbody>
- <tr>
- <td style="padding: 20px;text-align: center;">
- <a href="http://www.wonyen.com/" target="_blank"><img style="width: 100px;height: 100px;" src="http://wonyen.com/Images/qrcode_for_wonyen_258.jpg" border="0"></a>
- <a href="http://www.wonyen.com/" target="_blank"><img style="width: 100px;height: 100px;" src="http://wonyen.com/Images/qrcode_for_ifncn_258.jpg" border="0"></a>
- </td>
- </tr>
- <tr>
- <td style="font-size: 14px;text-align: center;">扫一扫,关注逸品商城微信公众号、订阅号</td>
- </tr>
- </tbody>
- </table>
- </td>
- <td style="width:360px;height:20px;text-align:center;padding:0px 0px 0px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">
- <table style="width:360px;">
- <tbody>
- <tr>
- <td style="padding: 20px;text-align: center;">
- <a href="http://www.wonyen.com/" target="_blank"><img style="height: 100px;" src="http://wonyen.com/Images/wechat_foot_img.png" border="0"></a>
- </td>
- </tr>
- <tr>
- <td style="font-size: 14px;">逸品商城---逸同分享 品质生活</td>
- </tr>
- </tbody>
- </table>
- </td>
- </tr>
- </tbody>
- </table>
- </td>
- </tr>
- <tr style="height: 10px;"></tr>
- <tr>
- <td style="width:720px;height:23px;text-align:center;padding:0px 0px 5px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;"><img style="height: 45px;" src="http://www.wonyen.com/Images/logo_mail_gray.png" border="0"></td>
- </tr>
- <tr style="height: 5px;"></tr>
- <tr>
- <td style="width:720px;height:20px;text-align:center;padding:0px 0px 30px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">文逸科技 版权所有 Copyright © 2016 闽ICP备06033317号 </td>
- </tr>
- </tbody>
- </table>
- </html>
模板的编写与html文件类似,还可以试用循环。仔细看上述内容就可以发现。
5.最后,我们编写一个controller的方法来实现发送富文本邮件。
- package com.xdx.controller;
- import java.io.UnsupportedEncodingException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.annotation.Resource;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import org.apache.velocity.app.VelocityEngine;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.velocity.VelocityEngineUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.xdx.entity.TProduct;
- import com.xdx.service.ProductService;
- @Controller
- public class DemoController {
- @Resource(name = "mailSender")
- private JavaMailSender mailSender;
- @Resource(name = "velocityEngine")
- private VelocityEngine velocityEngine;
- @Resource(name = "productService")
- private ProductService productService;
- @ResponseBody
- @RequestMapping(value = "simpleMailDemo", produces = "text/html; charset=utf-8")
- public String simpleMailDemo() {
- SimpleMailMessage mail = new SimpleMailMessage();
- JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
- // 发送方
- mail.setFrom(mailSenderImpl.getUsername());
- // 接收方
- mail.setTo("2717814812@qq.com");
- // 标题
- mail.setSubject("spring mvc简单邮件发送");
- // 设置邮件正文
- mail.setText("spring mvc简单邮件发送,收到此邮件表明邮件模块配置成功");
- mailSender.send(mail);
- return "成功";
- }
- @ResponseBody
- @RequestMapping(value = "velocityMailDemo", produces = "text/html; charset=utf-8")
- public String velocityMailDemo() throws UnsupportedEncodingException,
- MessagingException {
- JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
- MimeMessage mimeMessage = mailSenderImpl.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
- helper.setFrom(mailSenderImpl.getUsername(), "逸品周刊");
- helper.setTo("2717814812@qq.com");
- // 主题
- helper.setSubject("velocity模板邮件发送");
- String template = "test.vm";
- List<TProduct> productList = productService.getHotProductList();//获取商品列表
- Map<String, Object> model = new HashMap<String, Object>();
- model.put("productList", productList);
- String result = null;
- try {
- result = VelocityEngineUtils.mergeTemplateIntoString(
- velocityEngine, template, "UTF-8", model);
- } catch (Exception e) {
- }
- helper.setText(result, true);
- try {
- mailSenderImpl.send(mimeMessage);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "成功";
- }
- }
6.测试结果,图片是因为没有上传,所以没有显示出来。
7.最后看一下,整个项目的目录。
使用javaMail和velocity来发送模板邮件的更多相关文章
- SpringBoot整合Mail发送邮件&发送模板邮件
整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...
- spring 5.x 系列第19篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (xml配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置文件为springApplic ...
- 循序渐进VUE+Element 前端应用开发(33)--- 邮件参数配置和模板邮件发送处理
在系统处理中,有时候需要发送邮件通知用户,如新增用户的邮件确认,密码找回,以及常规订阅消息.通知等内容处理,都可以通过邮件的方式进行处理.本篇随笔介绍结合VUE+Element 前端,实现系统的邮件参 ...
- spring boot发送其他邮件
前面已经讲了使用springboot采用常规的javaweb方式发送邮件和使用spring模板发送邮件.但是发送的都是文本文件,现在来说一下使用spring模板发送一些其他的邮件. 1.pom.xml ...
- .net邮件发送实例 邮件内容为网页模板
.net邮件发送实例 邮件内容为网页模板 2009-07-03 09:31:01| 分类: .NET|字号 订阅 Encoding encoding = Encoding.GetEncod ...
- flask 电子邮件进阶实践-用模板发送163邮件
电子邮件进阶实践 下面来学习构建邮件的HTML正文,并使用模板组织内容. 一封电子邮件的正文可以是纯文本(text/plain),也可以是HTML格式的文本(text/html).处于全面的考虑,一封 ...
- spring 5.x 系列第20篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置类为com.heibaiyin ...
- 使用freemarker做邮件发送模板
1.解析工具类 package com.example.springbootfreemarker.utils; import freemarker.template.Configuration; im ...
- JavaMail(二):利用JavaMail发送复杂邮件
上一篇文章我们学习了利用JavaMail发送简单邮件,这篇文章我们利用JavaMail发送稍微复杂一点的邮件(包含文本.图片.附件).这里只贴出核心代码,其余代码可参考JavaMail(一):利用Ja ...
随机推荐
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller //控制类 service //服务接口 service.impl //服务 ...
- JS中!=、==、!==、===的用法和区别
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较"转化成同一类型后的值"看"值"是否相等,===如果类型不同 ...
- 【转】Entity Framework 5.0系列之约定配置
Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来复杂,大家也都试图将软件设计的越来越灵活,很多内容我们都希望是可配置的, ...
- 【玩转树莓派】使用 sinopia 搭建私有 npm 服务器
简介 使用 sinopia 的好处是,node系的工程师,内部协作时,使用自有 npm 包,会非常方便:另外,sinopia,会缓存已经下载过的包,可以在相当程度上,加速 npm install 相关 ...
- Windows下安装BeautifulSoup
python版本为2.7 1.去官网下载BeautifulSoup4 Beautiful Soup 4.3.2 2.解压文件 将下载得到的压缩包解压到任意文件夹,路径不含中文 3.打开cmd命令提示符 ...
- 在JQuery中如何获取当前时间?
////发表时间(now) function p(s) { return s < 10 ? '0' + s : s; } var myDate = new Date(); //获取当前年 var ...
- .Net Core 2.0 EntityFrameworkCore CodeFirst入门教程
最近难得有时间闲下来,研究了一下.net core 2.0,总的来说,目前除了一些第三方的库不支持外,基本上可以满足我们的项目需求了! 我们就以一个网站开发为例,搭建一个简单的三层架构,先熟悉一下.n ...
- 程序员的自我救赎---1.4.2: 核心框架讲解(BLL&Tool)
<前言> <目录> (一) Winner2.0 框架基础分析 (二) 短信中心 (三)SSO单点登录 (四)PLSQL报表系统 (五)钱包系统 (六)GPU支付中心 (七)权限 ...
- TreeSet(一)--排序
TreeSet(一) 一.TreeSet定义: 与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的. 1)TreeSet类概述 ...
- Idea Live Templates代码模板
一. 概念 创建代码模板进行快速代码编写,如sout-->System.out.println();. 如我们经常要写logger的定义:private static final Logger ...