之前在ssh项目中有用过javaMail和velocity来发送邮件,实现的效果如下所示。

这类邮件主要用于公司的推广宣传,比如商城的促销等场景。

今天打算将邮件模块也集成到ssm项目,也算是对之前做的东西的一种巩固。

简单邮件模块

首先来集成简单的邮件模块。

1.第一步,加jar包,在maven的pom.xml加入如下代码。

  1. <!-- 邮件 -->
  2. <dependency>
  3. <groupId>javax.mail</groupId>
  4. <artifactId>javax.mail-api</artifactId>
  5. <version>1.6.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.sun.mail</groupId>
  9. <artifactId>smtp</artifactId>
  10. <version>1.6.0</version>
  11. <type>jar</type>
  12. <scope>compile</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.sun.mail</groupId>
  16. <artifactId>mailapi</artifactId>
  17. <version>1.6.0</version>
  18. <type>jar</type>
  19. <scope>compile</scope>
  20. </dependency>

2.编写mail.properties配置文件

  1. #邮箱参数配置
  2. mail.host:smtp.qq.com
  3. mail.port:25
  4. mail.username:xxxxx
  5. mail.password:xxxxxx

3.在spring主配置文件中引入此配置文件

  1. <!-- 读取properties文件 参数配置,所有的参数配置均可放在此处 -->
  2. <bean id="propertyConfigurer"
  3. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  4. <property name="locations">
  5. <list>
  6. <value>classpath:jdbc.properties</value>
  7. <value>classpath:mail.properties</value>
  8. </list>
  9. </property>
  10. </bean>

4.新建spring-mail.xml文件,用于配置邮件相关的内容。

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. 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 ">
  6. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  7. <property name="host" value="${mail.host}" />
  8. <property name="port" value="${mail.port}" />
  9. <property name="username" value="${mail.username}" />
  10. <property name="password" value="${mail.password}" />
  11. <property name="javaMailProperties">
  12. <props>
  13. <prop key="mail.smtp.auth">true</prop>
  14. </props>
  15. </property>
  16. </bean>
  17. </beans>

5.在主配置文件中引入spring-mail.xml配置文件。

  1. <import resource="spring-mail.xml" />

6.在一个controller类中编码调用邮件,注意需要注入mailSender这个bean.

  1. @Resource(name = "mailSender")
  2. private JavaMailSender mailSender;
  1. @ResponseBody
  2. @RequestMapping(value = "simpleMailDemo", produces = "text/html; charset=utf-8")
  3. public String simpleMailDemo() {
  4. SimpleMailMessage mail = new SimpleMailMessage();
  5. JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
  6. // 发送方
  7. mail.setFrom(mailSenderImpl.getUsername());
  8. // 接收方
  9. mail.setTo("2717814812@qq.com");
  10. // 标题
  11. mail.setSubject("spring mvc简单邮件发送");
  12. // 设置邮件正文
  13. mail.setText("spring mvc简单邮件发送,收到此邮件表明邮件模块配置成功");
  14. mailSender.send(mail);
  15. return "成功";
  16. }

7.在浏览器输入http://192.168.1.185:8080/warrior/simpleMailDemo,结果如图。

看看邮箱,确实收到了邮件。

 采用velocity模板编写富文本邮件

1.第一步,引入velocity所需要用到的jar文件。

  1. <dependency>
  2. <groupId>org.apache.velocity</groupId>
  3. <artifactId>velocity</artifactId>
  4. <version>1.7</version>
  5. </dependency>

2.在spring-mail.xml中加入关于velocity的配置。

  1. <bean id="velocityEngine"
  2. class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  3. <property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!--
  4. 模板存放的路径 -->
  5. <property name="configLocation" value="classpath:velocity.properties" />
  6. </bean>
  7. <!--配置试图解析器 -->
  8. <bean id="viewResolver"
  9. class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  10. <property name="cache" value="false" />
  11. <property name="prefix" value="" />
  12. <property name="suffix" value=".vm" />
  13. <property name="contentType" value="text/html;charset=utf-8" />
  14. <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" />
  15. <property name="exposeSpringMacroHelpers" value="true" />
  16. <property name="exposeRequestAttributes" value="true" />
  17. <property name="exposeSessionAttributes" value="true" />
  18. <property name="allowSessionOverride" value="true" />
  19. <property name="allowRequestOverride" value="true" />
  20. </bean>

注意上述配置我们将velocity的模板存在了web-inf下的templates文件夹下,并且启用了toolbox.xml配置,toolbox.xml主要是一些工具,你可以把你在编写velocity模板的时候可能会用到的工具填写到toolbox.xml中。

3.注意上述配置文件引入了velocity.properties文件,在resources下新建velocity.properties文件。

  1. input.encoding=UTF-8
  2. output.encoding=UTF-8

4.在web-inf下新建templates文件夹,新建toolbox.xml,此处我随便写了两个工具到toolbox.xml里面。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <toolbox>
  3. <tool>
  4. <key>UrlUtil</key>
  5. <class>com.nali.common.util.UrlUtil</class>
  6. <scope>application</scope>
  7. </tool>
  8. <tool>
  9. <key>VersionUtil</key>
  10. <class>com.ximalaya.shop.api.domain.util.VersionUtil</class>
  11. <scope>application</scope>
  12. </tool>
  13. </toolbox>

4.编写velocity模板,在templates文件夹下面新建一个名为test.vm的模板。

  1. <html>
  2. <table style="display:none"><tr><td>逸品商城,优质农产品直供平台,会员专享,按需定制,个性化定产品,大宗商品,会员卡,宅配卡,逸品资讯,营养师服务,逸品云创亲子体验馆|WONYEN.COM</td></tr></table>
  3. <table align="center" cellpadding="0" cellspacing="0" style="width:720px;border:0px;background-color:White;margin:0 auto;padding:10px 0px 0px 0px;">
  4. <tbody>
  5. <tr>
  6. <td style="width:720px;height:40px;">
  7. <table style="width:720px;height:40px;">
  8. <tbody>
  9. <tr>
  10. <td style="width:130px;vertical-align:top;">
  11. <a href="http://www.wonyen.com" target="_blank"><img src="http://wonyen.com/Images/logo_mail.png" border="0" width="160" height="40"></a>
  12. </td>
  13. <td style="width:230px;"></td>
  14. <td style="width:90px;text-align:center;">
  15. <a href="http://wonyen.com/" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">首页</a>
  16. </td>
  17. <td style="width:90px;text-align:center;">
  18. <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>
  19. </td>
  20. <td style="width:90px;text-align:center;">
  21. <a href="http://bbs.wonyen.com/" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">会员社区 </a>
  22. </td>
  23.  
  24. <td style="width:90px;text-align:center;">
  25. <a href="http://wonyen.com/yipinStreet" target="_blank" style="text-decoration:none;color:#333333;font-size:12pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif">逸品街 </a>
  26. </td>
  27. </tr>
  28. </tbody>
  29. </table>
  30. </td>
  31. </tr>
  32. <tr style="height: 15px;"></tr>
  33. <tr>
  34. <td style="height: 40px;color: #282828;font-size: 16pt;font-weight: 700;">
  35. 逸品<span style="color: #f6531a;">资讯®</span>
  36. </td>
  37. </tr>
  38. <!--产品列表-->
  39. #foreach($product in $productList)
  40. <tr>
  41. <td style="width:720px;height:100px;padding:20px 0px 15px 0px;border-bottom-color: #ccc;border-bottom-width: 1px;border-bottom-style: dotted;">
  42. <table style="width:720px;">
  43. <tbody>
  44. <tr>
  45. <td rowspan="2" style="width:138.9px;">
  46. <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>
  47. </td>
  48. <td style="width:570px;height:20px;padding-left:10px;">
  49. <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>
  50. </td>
  51. </tr>
  52. <tr>
  53. <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>
  54. </tr>
  55. </tbody>
  56. </table>
  57. </td>
  58. </tr>
  59. #end
  60.  
  61. <tr>
  62. <td>
  63. <table>
  64. <tbody>
  65. <tr>
  66. <td style="width:360px;height:20px;text-align:center;padding:0px 0px 0px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">
  67. <table style="width:360px;">
  68. <tbody>
  69. <tr>
  70. <td style="padding: 20px;text-align: center;">
  71. <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>
  72. <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>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td style="font-size: 14px;text-align: center;">扫一扫,关注逸品商城微信公众号、订阅号</td>
  77. </tr>
  78. </tbody>
  79. </table>
  80. </td>
  81. <td style="width:360px;height:20px;text-align:center;padding:0px 0px 0px 0px;font-size:9pt;font-family:'微软雅黑',Arial,Helvetica,sans-serif;">
  82. <table style="width:360px;">
  83. <tbody>
  84. <tr>
  85. <td style="padding: 20px;text-align: center;">
  86. <a href="http://www.wonyen.com/" target="_blank"><img style="height: 100px;" src="http://wonyen.com/Images/wechat_foot_img.png" border="0"></a>
  87. </td>
  88. </tr>
  89. <tr>
  90. <td style="font-size: 14px;">逸品商城---逸同分享 品质生活</td>
  91. </tr>
  92. </tbody>
  93. </table>
  94. </td>
  95. </tr>
  96. </tbody>
  97. </table>
  98. </td>
  99. </tr>
  100. <tr style="height: 10px;"></tr>
  101. <tr>
  102. <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>
  103. </tr>
  104. <tr style="height: 5px;"></tr>
  105. <tr>
  106. <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>
  107. </tr>
  108. </tbody>
  109. </table>
  110. </html>

模板的编写与html文件类似,还可以试用循环。仔细看上述内容就可以发现。

5.最后,我们编写一个controller的方法来实现发送富文本邮件。

  1. package com.xdx.controller;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import javax.annotation.Resource;
  9. import javax.mail.MessagingException;
  10. import javax.mail.internet.MimeMessage;
  11.  
  12. import org.apache.velocity.app.VelocityEngine;
  13. import org.springframework.mail.SimpleMailMessage;
  14. import org.springframework.mail.javamail.JavaMailSender;
  15. import org.springframework.mail.javamail.JavaMailSenderImpl;
  16. import org.springframework.mail.javamail.MimeMessageHelper;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.ui.velocity.VelocityEngineUtils;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21.  
  22. import com.xdx.entity.TProduct;
  23. import com.xdx.service.ProductService;
  24. @Controller
  25. public class DemoController {
  26. @Resource(name = "mailSender")
  27. private JavaMailSender mailSender;
  28. @Resource(name = "velocityEngine")
  29. private VelocityEngine velocityEngine;
  30. @Resource(name = "productService")
  31. private ProductService productService;
  32.  
  33. @ResponseBody
  34. @RequestMapping(value = "simpleMailDemo", produces = "text/html; charset=utf-8")
  35. public String simpleMailDemo() {
  36. SimpleMailMessage mail = new SimpleMailMessage();
  37. JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
  38. // 发送方
  39. mail.setFrom(mailSenderImpl.getUsername());
  40. // 接收方
  41. mail.setTo("2717814812@qq.com");
  42. // 标题
  43. mail.setSubject("spring mvc简单邮件发送");
  44. // 设置邮件正文
  45. mail.setText("spring mvc简单邮件发送,收到此邮件表明邮件模块配置成功");
  46. mailSender.send(mail);
  47. return "成功";
  48. }
  49.  
  50. @ResponseBody
  51. @RequestMapping(value = "velocityMailDemo", produces = "text/html; charset=utf-8")
  52. public String velocityMailDemo() throws UnsupportedEncodingException,
  53. MessagingException {
  54. JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
  55. MimeMessage mimeMessage = mailSenderImpl.createMimeMessage();
  56. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
  57. helper.setFrom(mailSenderImpl.getUsername(), "逸品周刊");
  58. helper.setTo("2717814812@qq.com");
  59. // 主题
  60. helper.setSubject("velocity模板邮件发送");
  61. String template = "test.vm";
  62. List<TProduct> productList = productService.getHotProductList();//获取商品列表
  63. Map<String, Object> model = new HashMap<String, Object>();
  64. model.put("productList", productList);
  65. String result = null;
  66. try {
  67. result = VelocityEngineUtils.mergeTemplateIntoString(
  68. velocityEngine, template, "UTF-8", model);
  69. } catch (Exception e) {
  70. }
  71. helper.setText(result, true);
  72. try {
  73. mailSenderImpl.send(mimeMessage);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return "成功";
  78. }
  79. }

6.测试结果,图片是因为没有上传,所以没有显示出来。

7.最后看一下,整个项目的目录。

使用javaMail和velocity来发送模板邮件的更多相关文章

  1. SpringBoot整合Mail发送邮件&发送模板邮件

    整合mail发送邮件,其实就是通过代码来操作发送邮件的步骤,编辑收件人.邮件内容.邮件附件等等.通过邮件可以拓展出短信验证码.消息通知等业务. 一.pom文件引入依赖 <dependency&g ...

  2. spring 5.x 系列第19篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置文件为springApplic ...

  3. 循序渐进VUE+Element 前端应用开发(33)--- 邮件参数配置和模板邮件发送处理

    在系统处理中,有时候需要发送邮件通知用户,如新增用户的邮件确认,密码找回,以及常规订阅消息.通知等内容处理,都可以通过邮件的方式进行处理.本篇随笔介绍结合VUE+Element 前端,实现系统的邮件参 ...

  4. spring boot发送其他邮件

    前面已经讲了使用springboot采用常规的javaweb方式发送邮件和使用spring模板发送邮件.但是发送的都是文本文件,现在来说一下使用spring模板发送一些其他的邮件. 1.pom.xml ...

  5. .net邮件发送实例 邮件内容为网页模板

    .net邮件发送实例 邮件内容为网页模板 2009-07-03 09:31:01|  分类: .NET|字号 订阅      Encoding encoding = Encoding.GetEncod ...

  6. flask 电子邮件进阶实践-用模板发送163邮件

    电子邮件进阶实践 下面来学习构建邮件的HTML正文,并使用模板组织内容. 一封电子邮件的正文可以是纯文本(text/plain),也可以是HTML格式的文本(text/html).处于全面的考虑,一封 ...

  7. spring 5.x 系列第20篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (代码配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置类为com.heibaiyin ...

  8. 使用freemarker做邮件发送模板

    1.解析工具类 package com.example.springbootfreemarker.utils; import freemarker.template.Configuration; im ...

  9. JavaMail(二):利用JavaMail发送复杂邮件

    上一篇文章我们学习了利用JavaMail发送简单邮件,这篇文章我们利用JavaMail发送稍微复杂一点的邮件(包含文本.图片.附件).这里只贴出核心代码,其余代码可参考JavaMail(一):利用Ja ...

随机推荐

  1. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  2. JS中!=、==、!==、===的用法和区别

    1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较"转化成同一类型后的值"看"值"是否相等,===如果类型不同 ...

  3. 【转】Entity Framework 5.0系列之约定配置

    Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来复杂,大家也都试图将软件设计的越来越灵活,很多内容我们都希望是可配置的, ...

  4. 【玩转树莓派】使用 sinopia 搭建私有 npm 服务器

    简介 使用 sinopia 的好处是,node系的工程师,内部协作时,使用自有 npm 包,会非常方便:另外,sinopia,会缓存已经下载过的包,可以在相当程度上,加速 npm install 相关 ...

  5. Windows下安装BeautifulSoup

    python版本为2.7 1.去官网下载BeautifulSoup4 Beautiful Soup 4.3.2 2.解压文件 将下载得到的压缩包解压到任意文件夹,路径不含中文 3.打开cmd命令提示符 ...

  6. 在JQuery中如何获取当前时间?

    ////发表时间(now) function p(s) { return s < 10 ? '0' + s : s; } var myDate = new Date(); //获取当前年 var ...

  7. .Net Core 2.0 EntityFrameworkCore CodeFirst入门教程

    最近难得有时间闲下来,研究了一下.net core 2.0,总的来说,目前除了一些第三方的库不支持外,基本上可以满足我们的项目需求了! 我们就以一个网站开发为例,搭建一个简单的三层架构,先熟悉一下.n ...

  8. 程序员的自我救赎---1.4.2: 核心框架讲解(BLL&Tool)

    <前言> <目录> (一) Winner2.0 框架基础分析 (二) 短信中心 (三)SSO单点登录 (四)PLSQL报表系统 (五)钱包系统 (六)GPU支付中心 (七)权限 ...

  9. TreeSet(一)--排序

    TreeSet(一) 一.TreeSet定义:      与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的.            1)TreeSet类概述 ...

  10. Idea Live Templates代码模板

    一. 概念 创建代码模板进行快速代码编写,如sout-->System.out.println();. 如我们经常要写logger的定义:private static final Logger ...