利用Velocity结合Spring发email
在spring中发mail是一件容易的事,如果利用Velocity做mail的模板来发送就更得心应手了。
首先,还是简单描述sping中的配置,发mail需要一个mail的engin:
<bean id="mailEngine" class="com.aodragon.jdt.backend.service.impl.MailEngineImpl">
<property name="mailSender">
<ref local="mailSender"/>
</property>
<property name="velocityEngine">
<ref local="velocityEngine"/>
</property>
</bean>
实现的类如下:
package com.aodragon.jdt.backend.service.impl;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.*;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.*;
import org.springframework.mail.javamail.*;
import org.springframework.ui.velocity.VelocityEngineUtils;
import com.aodragon.jdt.backend.service.MailEngine;
public class MailEngineImpl implements MailEngine {
protected static final Log log = LogFactory.getLog(MailEngineImpl.class);
private MailSender mailSender;
private VelocityEngine velocityEngine;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public void sendMessage(SimpleMailMessage msg, String templateName,
Map model) {
String result = null;
try {
result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, model);
} catch (VelocityException e) {
e.printStackTrace();
}
msg.setText(result);
send(msg);
}
public void sendMessage(SimpleMailMessage msg, String templateName, String encoding, Map model) {
String result = null;
try {
result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, encoding,model);
} catch (VelocityException e) {
e.printStackTrace();
}
msg.setText(result);
send(msg);
}
public void send(SimpleMailMessage msg) {
try {
mailSender.send(msg);
} catch (MailException ex) {
//log it and go on
log.error(ex.getMessage());
}
}
public void sendMessage(String[] emailAddresses,
ClassPathResource resource, String bodyText,
String subject, String attachmentName)
throws MessagingException {
MimeMessage message =
((JavaMailSenderImpl) mailSender).createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(emailAddresses);
helper.setText(bodyText);
helper.setSubject(subject);
helper.addAttachment(attachmentName, resource);
((JavaMailSenderImpl) mailSender).send(message);
}
}
这个engin实现了4个方法,在需要的时候调用,这里也需要两个对象:
private MailSender mailSender;
private VelocityEngine velocityEngine;
所以在配置的时候需要注意有:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${email.host}</value>
</property>
<property name="username">
<value>${email.username}</value>
</property>
<property name="password">
<value>${email.password}</value>
</property>
</bean>
<bean id="velocityEngine" 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>
<prop key="velocimacro.library"/>
</props>
</property>
</bean>
这样配置是完成了,下面给一个在Action中发mail的调用示例
public ActionForward save(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletRresponse) throws
Exception {
String toEmail = "…"
HashMap model = new HashMap();
this.smm.setTo(toEmail);
this.smm.setSubject("谢谢您的关注");
model.put("content","………….. ");
MessageResources resources = getResources(httpServletRequest);
this.mailEngin.sendMessage(this.smm,resources.getMessage(_locale,"velocity.email.bookproduct"),model);
return list(actionMapping, actionForm, httpServletRequest,
httpServletRresponse);
} // end save
这里需要说明的就是调用sendMessage中的第2个参数为resources.getMessage(_locale,"velocity.email.bookproduct"),这里是为了适应多语言版本,调用对应的vm文件,velocity.email.bookproduct是保存在ApplicationResource.properties各个版本中的vm文件对应的路径。其中_locale = (Locale)request.getSession().getAttribute(Globals.LOCALE_KEY)。
这样,简单的Velocity结合Spring发送email就完成了,还可以添加一个TASK来管理邮件的队列,让系统定时发送队列中的邮件;在线管理mailSender中的各个参数等等。
利用Velocity结合Spring发email的更多相关文章
- 第19章-使用Spring发送Email
1 配置Spring发送邮件 Spring Email抽象的核心是MailSender接口.顾名思义,MailSender的实现能够通过连接Email服务器实现邮件发送的功能,如图19.1所示. 图1 ...
- linux mail利用外部邮箱地址发邮件
mail命令发送邮件需要sendmail或postfix服务 三种常用格式发信 mail -s "标题" xxx@xxx.xxx #第一种方法,你可以把当前shell当成编辑器来用 ...
- 利用阿里大于接口发短信(Delphi版)
阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信.语音.流量直充.私密专线.店铺手机号等个性化服务.每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到.官方文档提供 ...
- 自我救赎 → 利用 IDEA 和 Spring Boot 搭建 SSM
前言 开心一刻 儿子读高中放学回来了,一向不管他学习的我突然来了兴趣,想看看他的学习他的状况,抄起他的数学习题看了起来,当看到 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x ...
- 使用Spring发送Email
配置Spring发送邮件 Spring发送邮件底层还是使用JavaMail,我在http://www.cnblogs.com/lz2017/p/6882925.html 中记录过关于JavaMail的 ...
- Spring 发送 Email
本文转自:http://zl198751.iteye.com/blog/757617 看到了本文,收获颇丰,感谢之至! 首先介绍下Email的发送流程: 需要选中smtp邮件服务器,Yahoo不提供免 ...
- 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)
Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference
Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify ...
随机推荐
- Codeforce Round #222 Div2
这场断网,本来有个别人的比较卡的无线 但后面睡着了- -! C:额,逆向想下! B:... A:...
- bzoj3489 A simple rmq problem 可持久化树套树
先预处理出两个个数组pre,next.pre[i]表示上一个与i位置数字相同的位置,若不存在则设为0:next[i]表示下一个与i位置数字相同的位置,若不存在则设为n+1.那么一个满足在区间[L,R] ...
- TP隐藏入口
我们知道,在thinkphp的案例中有一个.htaccess文件,里面配置了URL的一些重写规则,如: <IfModule mod_rewrite.c> RewriteEngine on ...
- jquery on 动态添加的元素,神奇的bug
$(document.body).on("click", ".comments-item .link-comment", function () { 平时用 d ...
- SQL Server XML基础学习之<7>--XML modify() 方法对 XML 数据中插入、更新或删除
/*------------------------------------------------------------------------------+ #| = : = : = : = : ...
- js构造函数和继承实现方式
- imread函数、namedWindow函数、imshow函数、imwrite函数
1.imread函数 首先,我们看imread函数,可以在OpenCV官方文档中查到其原型如下: Mat imread(const string& filename, int flags=1 ...
- mybatis 相关总结
1. Mybatis的<where><foreach><set>等标签详解:http://blog.csdn.net/zenson_g/article/detail ...
- selenium定位失败记录
selenium webdriver定位不到元素的五种原因及解决办法 1.动态id定位不到元素 for example: //WebElement xiexin_element = driver.fi ...
- Delphi的DateToStr StrToDate格式灵活用法
Delphi的DateToStr StrToDate格式灵活用法 2008-04-09 10:19 procedure TForm1.Button1Click(Sender: TObject);var ...