Java - Java Mail邮件开发(3)spring +Java Mail + Velocity
1、spring + Java Mail + Velocity
项目结构:

注意:用户包中引入各包的顺序问题。如velocity-2.1。
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="mail.properties"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="username" value="${mail.user}"/>
<property name="password" value="${mail.pwd}"/>
</bean> <bean id="javaMailSenderService" class="com.lfy.sendmail.JavaMailSenderService"/>
<bean id="javaMailSenderImplService" class="com.lfy.sendmail.JavaMailSenderImplService"/> <!-- VelocityEngineFactory -->
<bean id="velocityEngineFactory" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
</props>
</property>
</bean> <bean id="velocityEngineService" class="com.lfy.velocity.VelocityEngineService"/> </beans>
VelocityEngineService.java
package com.lfy.velocity; import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired; public class VelocityEngineService { @Autowired
private VelocityEngine velocityEngine; public VelocityEngine getVelocityEngine() {
return velocityEngine;
} public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
}
JavaMailSenderImplService.java
package com.lfy.sendmail; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl; /**
* 2、spring支持的第二种发送器JavaMailSenderImpl
* @author lfy
*
*/
public class JavaMailSenderImplService { @Autowired
private JavaMailSenderImpl javaMailSenderImpl; public JavaMailSenderImpl getJavaMailSenderImpl() {
return javaMailSenderImpl;
} public void setJavaMailSenderImpl(JavaMailSenderImpl javaMailSenderImpl) {
this.javaMailSenderImpl = javaMailSenderImpl;
} /**
* simple content
* @param message
*/
public void send(SimpleMailMessage message){
javaMailSenderImpl.send(message);
System.out.println("JavaMailSenderImpl:send silpleMessage successfully.");
} /**
* Velocity content
* @param message
*/
public void sendWithVelocity(MimeMessage message) {
javaMailSenderImpl.send(message);
System.out.println("JavaMailSenderImpl:send mimeMessage successfully.");
}
}
index.vm
<html>
<head>
<style type="text/css">
h4{
color:red;
background:#efefef;
}
</style>
</head>
<body>
<h4>${user} </h4>
<p><p>
<i>${content}</i>
</body>
</html>
springJavaMailSender.java
package com.lfy.main; import java.util.HashMap;
import java.util.Map; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils; import com.lfy.sendmail.JavaMailSenderImplService;
import com.lfy.velocity.VelocityEngineService; public class springJavaMailSender { public static void main(String[] agrs) throws MessagingException {
//创建spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
JavaMailSenderImplService javaMailSenderImplService = (JavaMailSenderImplService)ctx.getBean("javaMailSenderImplService"); VelocityEngine velocityEngine=(VelocityEngine)((VelocityEngineService)ctx.getBean("velocityEngineService")).getVelocityEngine();
Map<String,Object> model=new HashMap<String,Object>();
model.put("user", "Tomcat");
model.put("content", "Hello");
String emailText=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "index.vm", "UTF-8", model);
MimeMessage msg=javaMailSenderImplService.getJavaMailSenderImpl().createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,true);
helper.setFrom("xxxxxxxxxx@163.com");
helper.setTo("xxxxxxxx@qq.com");
helper.setCc("xxxxxxxx@163.com");
helper.setSubject("Velocity模板测试");
helper.setText(emailText, true);
javaMailSenderImplService.sendWithVelocity(msg);
}
}
运行效果:

2、使用Maven配置使用Velocity。整理中...
3、Velocity加载模板的3中方式
1》从文件加载模板文件(默认方式)
properties.setProperty("resource.loader", "file");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
2》从类路径加载模板文件
properties.setProperty("resource.loader", "class");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
3》从jar文件中加载模板文件
properties.setProperty("resource.loader", "jar");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
//设置jar包所在的位置
properties.setProperty("jar.resource.loader.path", "jar:file:/F:/quicksure_Server_Provider.jar");
Java - Java Mail邮件开发(3)spring +Java Mail + Velocity的更多相关文章
- 用java自带jdk开发第一个java程序
[学习笔记] 1.用java自带jdk开发第一个java程序: 下面要讲的eclipse要想正常工作,需要先学会配置这里的jdk.jdk要想正常工作,需先学会配置JAVA_HOME和ClassPa ...
- Java - Java Mail邮件开发(2)springboot +Java Mail + Html
1.springboot + Java Mail + Html 项目结构: pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
- 【JavaWeb项目】一个众筹网站的开发(九)邮件开发
Java官方支持邮件开发,Javax-mail jdk中默认没有,需要另外下载 apache的基于Javax-mail开发了commons-mail,更加简单高效,推荐使用 一.电子邮件接收和发送协议 ...
- Java-JavaMail邮件开发(1)Java Mail
1.仅使用Java Mail 使用163邮箱作为代理服务器: 引入依赖: <dependency> <groupId>com.sun.mail</groupId> ...
- Spring进阶—如何用Java代码实现邮件发送(一)
相关文章: <Spring进阶—如何用Java代码实现邮件发送(二)> 在一些项目里面如进销存系统,对一些库存不足发出预警提示消息,招聘网站注册用户验证email地址等都需要用到邮件发送技 ...
- 传智播客张孝祥java邮件开发随笔01
01_传智播客张孝祥java邮件开发_课程价值与目标介绍 02_传智播客张孝祥java邮件开发_邮件方面的基本常识 03_传智播客张孝祥java邮件开发_手工体验smtp和pop3协议 第3课时 关于 ...
- Java Mail 邮件发送Demo
上周公司的项目要求开发邮件发送功能.自己在网上跟着教程边学边做了一下午,现在基本开发完成了.由于一个同事也想看下该怎么写,顺便学习下.所以我就写成了一遍教程,顺便巩固下邮件发送里面的内容. Demo ...
- java深入探究10-文件上传组件FileUpload,邮件开发
1.文件上传组件FileUpload 1)java提供了文件上传的工具包 需要引入:commons-fileupload-1.2.1.jar(文件上床组件核心包) commons-oi-1.4(封装了 ...
随机推荐
- mysql8.0.16二进制安装
mysql8.0.16二进制安装 环境简介操作系统:Centos 6.10 64位 目前版本:8.0.16 MySQL Community Server 二进制 安装目录:/data/mysql/my ...
- jmeter测试文件上传功能
最近为了完成自动化KPI开始慢慢接触jmeter,其中遇到了不少问题,今天就遇到了文件上传的问题,在这里记录下加深记忆,也可供jmeter初级使用者作为一个参考.另外论坛上已有同事发过关于jmeter ...
- 【NOIP2012模拟10.25】旅行
题目 给定一个n行m列的字符矩阵,'.'代表空地,'X'代表障碍.移动的规则是:每秒钟以上下左右四个方向之一移动一格,不能进入障碍. 计算:在空地中随机选择起点和终点(可以重合,此时最短耗时为0),从 ...
- 【leetcode】Reorganize String
题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...
- js-展开评论与隐藏评论
//控制展开评论和隐藏评论 controldiscuss(){ $(".opendiss").click(function(){ if($(this).context.innerH ...
- node 中process进程argv,argv0,execArgv,execPath
1.argv const {argv,argv0,execPath v,execPath} = process; argv.forEach(item=>{ console.log(item); ...
- SpringBoot项目中,表单的验证操作
在创建Springboot项目中,我们使用了表单验证操作,这一操作将极大地简化我们编程的开发 1.接收数据,以及验证 @PostMapping("/save") public Mo ...
- UVa 129 Krypton Factor (DFS && 回溯)
题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...
- Windows+anaconda+jupyter notebook+R+python3.6
Windows+anaconda+jupyter notebook+R+python3.6 环境配置 1. 设置国内清华大学镜像 打开 anaconda prompt,输入命令 conda confi ...
- cvpr 2019 workshop&oral session
1. Verification and Certification of Neural Networks神经网络的验证与认证 2. Automated Analysis of Marine Video ...