Use Velocity to generate HTML based email
来源:互联网 发布:linux socket封装 编辑:程序博客网 时间:2024/06/07 07:40
http://www.java2s.com/Code/Java/Velocity/UseVelocitytogenerateHTMLbasedemail.htm
import java.io.StringWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class EmailDemo
{
public static void main( String[] args )
throws Exception
{
/*
* first, get and initialize an engine
*/
VelocityEngine ve = new VelocityEngine();
ve.init();
/*
* organize our data
*/
ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "Cow");
map.put("price", "$100.00");
list.add( map );
map = new HashMap();
map.put("name", "Eagle");
map.put("price", "$59.99");
list.add( map );
map = new HashMap();
map.put("name", "Shark");
map.put("price", "$3.99");
list.add( map );
/*
* add that list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("petList", list);
/*
* get the Template
*/
Template t = ve.getTemplate( "./src/email_html.vm" );
/*
* now render the template into a Writer, here
* a StringWriter
*/
StringWriter writer = new StringWriter();
t.merge( context, writer );
/*
* use the output in the body of your emails
*/
System.out.println( writer.toString() );
}
}
-------------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Pet Store Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$petList.size() Pets on Sale!</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $pet in $petList )
<TR>
<TD>$count)</TD>
<TD>$pet.name</TD>
<TD>$pet.price</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<I>Call Today!</I>
Bests <br>
www.java2s.com
</CENTER>
</BODY>
</HTML>
===camel
ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "Cow");
map.put("price", "$100.00");
list.add( map );
map = new HashMap();
map.put("name", "Eagle");
map.put("price", "$59.99");
list.add( map );
map = new HashMap();
map.put("name", "Shark");
map.put("price", "$3.99");
list.add( map );
template.sendBody("direct:route1", list);
<route>
<from uri="direct:route1"/>
<to uri="log:route1?level=INFO"/>
<to uri="velocity:table.vm" />
<to uri="log:route1?level=INFO"/>
</route>
<HTML>
<HEAD>
<TITLE>Pet Store Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>${body}.size() Pets on Sale!</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $pet in ${body} )
<TR>
<TD>$count)</TD>
<TD>$pet.name</TD>
<TD>$pet.price</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<I>Call Today!</I>
Bests <br>
www.java2s.com
</CENTER>
</BODY>
</HTML>
- Use Velocity to generate HTML based email
- Selenium – Use Ant to Generate HTML Reports
- Use XSLT to generate HTML report from XML
- Use Rome to generate the RSS
- Use Java Mail API to Send Email
- How to use bindings-generator to generate lua binding automatically
- How to Use BSP Trees to Generate Game Maps
- Use EVP to generate random key and IV.
- How to use freetts create/generate a wave file
- Use mdev to automatically generate the device node
- [Python] Use Python to generate Java Unit Test codes automatically
- Spring v3.0.2 Learning Note 15 - Use Velocity Email Template with Spring
- How to Use Hive-based Registry IN WINCE.NET
- iFrame 项目总结 how to use Navigation-based application
- How to Use Hive-based Registry IN WINCE.NET
- Configuring git send-email to use Gmail SMTP
- Velocity&Email
- How to use Access Log Sampler to Generate Traffic with JMeter
- 史上最牛逼密码汇总
- [Android] SurfaceView使用实例
- XMM SSE2浮点指令
- 隐藏navigation 及 uitabbar
- 黑马韩前成linux从入门到精通の第二章Linux主机的规划
- Use Velocity to generate HTML based email
- PHP
- 不要被大而全所压倒
- 继承Label的重绘事件以实现竖排显示文字(含实例原码)
- 利用反射,得到javaBean中的属性名
- vc_各类型数据最大值
- 黑马韩前成linux从入门到精通の第三章Linux系统的安装
- Yii::记录日志到自定义文件
- 其他对象—System、Runtime、Date、Calendar、Math