- 浏览: 137889 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
admin666:
新人路过,表示对我有帮助
Activiti(新手)工作流对用户任务签收办理操作 -
汽车城路:
根据数据库里的字段来动态添加grid列,你实现了么?
动态对ext grid添加列
在这个例子中,将与发送方相关的配置信息放在了一个email.properties文件中,spring容器启动的时候会从这个属性文件中读取发送方的配置信息,这样配置的主要原因在于,发送方一般都是固定不变的。
我将发送的内容放在一个velocity模板文件中,这个文件很像jsp文件,您可以从http://airport.iteye.com/blog/23634中获取更多的关于velocity使用方法相关的信息。(实际上velocity是Apache提供的一种模板语言)。我希望邮件接收方能够接收一个动态的网页,而不是一个简单的文本。
另外,程序提供了发送附件的部分,和群发的功能,只不过群发功能并不完善,因为群发过程中无法将模板变量替换成用户相关变量值,这是个缺陷,楼主也没想明白如何实现。
下面将代码贴出,希望对大家有帮助:
先是email.properties文件:
模板文件:
简单解释一下,很明显这是个html文件,注意$!{userName}实际上是一个变量,在程序运行的时候,由model(一个hashmap类型的变量)传入的值会将其替换,(model的key就是变量的名字,这里是userName,而value就是要替换的值),!表示如果没有替换的值,此处为空。更多详细信息请参考:http://airport.iteye.com/blog/23634
下面是spring的配置文件:
ThirdVelocityEmailConfig.xml
这个大家都熟悉,不多解释,开始的时候加载了email.properties文件,我们上面给出了,然后创建了velocityEngine的bean。
下面是spring的配置类:
对于spring是否应该由类来配置,我并不关心,一般情况下,我更倾向于annotation这种形式,因为看起来比xml舒服点,但是annotation不能完全取代xml配置方式(至少现在是这样的),当然您完全可以使用xml进行bean的配置,效果是一样的。在这个配置类中,将从email.properties中读取的值赋值给了JavaMailSenderImpl,这个对象是java邮件发送的主要类,使用其send方法可以将预先准备好的消息发送出去。
下面给出发送服务的源码
主要是mailSend这个方法,在这个方法里先创建了一个消息,然后调用mailSender将消息发送出去。
最后给出测试程序:
这个程序对一个email来说功能已经很全面了,您可以对其更改然后应用于您的程序中。
再次简单说明一下,主机的信息配置在mail.properties文件中,发送的内容写在velocity模板文件中,在执行的时候将相应的字段进行替换。使用了MimeMessagePreparator进行复杂邮件的发送,MimeMessageHelper帮助简化邮件信息过程。解决了中文乱码问题。
转至:
http://www.iteye.com/wiki/blog/837369
我将发送的内容放在一个velocity模板文件中,这个文件很像jsp文件,您可以从http://airport.iteye.com/blog/23634中获取更多的关于velocity使用方法相关的信息。(实际上velocity是Apache提供的一种模板语言)。我希望邮件接收方能够接收一个动态的网页,而不是一个简单的文本。
另外,程序提供了发送附件的部分,和群发的功能,只不过群发功能并不完善,因为群发过程中无法将模板变量替换成用户相关变量值,这是个缺陷,楼主也没想明白如何实现。
下面将代码贴出,希望对大家有帮助:
先是email.properties文件:
email.host=smtp.163.com email.username=woshiguanxinquan email.password=********* mail.smtp.auth=true
模板文件:
Wecome.vm <html> <body> <h3>您好 $!{userName}, 欢迎您加入编程爱好者俱乐部!</h3> <div> 您的email地址是<a href="mailto:${emailAddress}">$!{emailAddress}</a>. 本条信息是系统自动发送,请勿回复! </div> </body> </html>
简单解释一下,很明显这是个html文件,注意$!{userName}实际上是一个变量,在程序运行的时候,由model(一个hashmap类型的变量)传入的值会将其替换,(model的key就是变量的名字,这里是userName,而value就是要替换的值),!表示如果没有替换的值,此处为空。更多详细信息请参考:http://airport.iteye.com/blog/23634
下面是spring的配置文件:
ThirdVelocityEmailConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:/com/guan/chapter19/email/email.properties"/> <context:annotation-config/> <context:component-scan base-package="com.guan.chapter19.email"/> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </value> </property> </bean> </beans>
这个大家都熟悉,不多解释,开始的时候加载了email.properties文件,我们上面给出了,然后创建了velocityEngine的bean。
下面是spring的配置类:
package com.guan.chapter19.email; import java.util.Properties; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; @Configuration public class ThirdVelocityEmailAppConfig { //从配置文件中读取相应的邮件配置属性 private @Value("${email.host}") String emailHost; private @Value("${email.username}") String userName; private @Value("${email.password}") String password; private @Value("${mail.smtp.auth}") String mailAuth; //JavaMailSender用来发送邮件的类 public @Bean JavaMailSender mailSender(){ JavaMailSenderImpl ms = new JavaMailSenderImpl(); ms.setHost(emailHost); ms.setUsername(userName); ms.setPassword(password); Properties pp = new Properties(); pp.setProperty("mail.smtp.auth", mailAuth); ms.setJavaMailProperties(pp); return ms; } }
对于spring是否应该由类来配置,我并不关心,一般情况下,我更倾向于annotation这种形式,因为看起来比xml舒服点,但是annotation不能完全取代xml配置方式(至少现在是这样的),当然您完全可以使用xml进行bean的配置,效果是一样的。在这个配置类中,将从email.properties中读取的值赋值给了JavaMailSenderImpl,这个对象是java邮件发送的主要类,使用其send方法可以将预先准备好的消息发送出去。
下面给出发送服务的源码
package com.guan.chapter19.email; import java.io.File; import java.util.Map; import javax.mail.internet.MimeMessage; import org.apache.velocity.app.VelocityEngine; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.stereotype.Component; import org.springframework.ui.velocity.VelocityEngineUtils; @Component public class ThirdVelocityEmailService { private JavaMailSender mailSender; private VelocityEngine velocityEngine; @Autowired public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } @Autowired public void setVelocityEngine(VelocityEngine velocityEngine) { this.velocityEngine = velocityEngine; } public void sendEmail(final Map<String,Object> model,final String subject,final String vmfile,final String[] mailTo,final String [] files) { MimeMessagePreparator preparator = new MimeMessagePreparator() { //注意MimeMessagePreparator接口只有这一个回调函数 public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,"GBK"); //这是一个生成Mime邮件简单工具,如果不使用GBK这个,中文会出现乱码 //如果您使用的都是英文,那么可以使用MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(mailTo);//设置接收方的email地址 message.setSubject(subject);//设置邮件主题 message.setFrom("woshiguanxinquan@163.com");//设置发送方地址 String text = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, vmfile,"GBK", model); //从模板中加载要发送的内容,vmfile就是模板文件的名字 //注意模板中有中文要加GBK,model中存放的是要替换模板中字段的值 message.setText(text, true); //将发送的内容赋值给MimeMessageHelper,后面的true表示内容解析成html //如果您不想解析文本内容,可以使用false或者不添加这项 FileSystemResource file; for(String s:files)//添加附件 { file = new FileSystemResource(new File(s));//读取附件 message.addAttachment(s, file);//向email中添加附件 } } }; mailSender.send(preparator);//发送邮件 } }
主要是mailSend这个方法,在这个方法里先创建了一个消息,然后调用mailSender将消息发送出去。
最后给出测试程序:
package com.guan.chapter19.email; import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ThirdVelocityEmailTest { @Test public void sendEmail() { ApplicationContext context = new ClassPathXmlApplicationContext("com/guan/chapter19/email/ThirdVelocityEmailConfig.xml"); ThirdVelocityEmailService tves = context.getBean(ThirdVelocityEmailService.class); Map<String,Object> model = new HashMap<String,Object>(); model.put("userName","大关"); model.put("emailAddress", "woshidaguan@126.com"); tves.sendEmail(model,"欢迎您的加入","com/guan/chapter19/email/welcome.vm",new String[]{"woshiguanxinquan@163.com"},new String[]{"F:/Sunset.jpg","F:/spring-hibernate.rar"}); //参数说明:替换velocity模板的变量和值对,邮件主题,velocity模板文件的路径,接收方email地址,附件 //简单说明,如果您要群发,可以在接收方email地址中多传入几个email地址,附件可以一次发送多个 } }
这个程序对一个email来说功能已经很全面了,您可以对其更改然后应用于您的程序中。
再次简单说明一下,主机的信息配置在mail.properties文件中,发送的内容写在velocity模板文件中,在执行的时候将相应的字段进行替换。使用了MimeMessagePreparator进行复杂邮件的发送,MimeMessageHelper帮助简化邮件信息过程。解决了中文乱码问题。
转至:
http://www.iteye.com/wiki/blog/837369
发表评论
-
Activiti(新手)工作流对用户任务签收办理操作
2016-03-30 17:43 23392首先是我们已经对Activiti流程的开启有了了解之后,在 ... -
Activiti(新手)工作流如何开启流程
2016-03-30 16:32 9746本文中简单介绍如何开启Activiti工作流引擎,百度上已 ... -
Java操作Oracle大字段(BLOB,CLOB)
2013-06-17 14:49 3659import java.io.*; import java.s ... -
Axis WebService 一看就懂
2012-12-07 14:54 1188axis1—4 webservices服务器客户端代码 -- ... -
异常com.ibatis.sqlmap.client.SqlMapClientBuilder
2011-12-27 20:30 2709针对spring3整合ibatis时发生的异常错误: Caus ... -
spring配置数据源
2011-12-22 11:43 877不管通过何种持久化技 ... -
Velocity用户手册---中文版(学习修改版)
2011-12-04 22:29 1847Velocity的能力远不止web站点开发这个领域,例如,它可 ... -
Spring3 + iBatis3 配置
2011-12-04 18:38 710原文连接 http://www.2cto.com/kf/ ... -
Jsp文件中播发视频文件
2011-12-02 19:02 945详情请转至:↓ http://blog.sina.com.cn ... -
JSON常用格式大处理
2011-10-31 14:43 7826总结了日常开发中对Json格式的处理,需要下载json相关Ja ... -
定义管理char字符处理格式
2011-10-31 14:37 896写这些东西;Ctrl-Cv的手酸; 不是太全面,希望大家共同 ... -
如何得到服务器路径(weblogic-domain)
2011-10-31 14:30 4618对于如何在weblogic中得到web项目路径问题,是很常见的 ... -
java根据文件类型写入
2011-10-31 14:24 941根据文件类型写入文件;参数方法一目了然 public S ... -
java去掉字符串的任何位置多余的空格
2011-10-17 16:27 1059用java去掉字符串的任何位置多余的空格,很经典。 代码如下 ... -
java-execl
2011-08-16 11:22 982解决方法: 1、新建excel文件,手动复制原文件内容 ... -
在JSp中直接使用分页
2010-05-21 16:53 1015没事出来逛逛。。。。 ...
相关推荐
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)
spring+velocity发送邮件,spring+velocity发送邮件
Velocity模板实例 Velocity模板实例 Velocity模板实例 Velocity模板实例
Velocity模板入门简单DEMO, 代码有注解;
Velocity模板技术语法详细介绍,包括很详细的示例代码 1.变量………………………………………………………………………………1 2.循环………………………………………………………………………………2 3.条件语句...
Spring Boot项目 自动生成数据表模板,包括:Mapper、Mapper.xml、Service、Controller、html 自定义模板生成
SpringBoot集成Mybatis,velocity模板
分享一个spring+mybatis+velocity项目demo,该项目是之前给一个学第学习用的,主要基于springMVC、mybatis、velocity搭建的,使用maven构建,其中zai service层编写了两个简单组件,一个是email发送,一个是认证授权...
Velocity.js 是velocity模板语法的javascript实现。Velocity 是基于Java的模板引擎,广泛应用在阿里集 体各个子公司。Velocity模板适用于大量模板使用的场景,支持复杂的逻辑运算,包含 基本数据类型、变量赋值和...
spring mvc与velocity整合
史上最全Velocity模板使用指南中文版,适合初学者读阅
Velocity模板解析,通过Velocity模板自动替换表达式中的变量
spring mvc mybatis velocity 示范
SpringBoot1.5以上版本兼容被抛弃的velocity,详情说明见项目README.md。 # springboot1.5.x+ velocity2 springboot maven 搭建 springboot1.5以上因不支持velocity 问题的核心在于高版本的springboot已经删除了...
spring+velocity+ibatis
Spring+SpringMVC+Mybatis+Velocity+Maven整合
测试报告模板+guice-3.0.jar+reportng-1.1.6.jar+velocity-dep-1.4.jar