java velocity 邮件_spring+velocity自动发送邮件

1.下载spring及velocity类库,

email配置文件:mail.properties:

mail.default.from=jfishsz@163.com

mail.host=smtp.163.com

mail.username=xxxxxx

mail.password=xxxxxx

mail.smtp.auth=true

mail.smtp.timeout=25000

spring配置文件:applicationContext.xml

/p>

"http://www.springframework.org/dtd/spring-beans.dtd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:mail.properties

class="org.springframework.mail.javamail.JavaMailSenderImpl">

${mail.host}

${mail.username}

${mail.password}

${mail.smtp.auth}

${mail.smtp.timeout}

class="org.springframework.mail.SimpleMailMessage"

singleton="false">

${mail.default.from}

class="org.springframework.ui.velocity.VelocityEngineFactoryBean">

class

org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

velocity模板文件:accountCreated.vm:

${message}

Username: ${username}

Password: ${Password}

Login at: ${applicationURL}

2.实现类

SendMail.java

package net.pms.email;

import java.util.Map;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.exception.VelocityException;

import org.springframework.mail.MailSender;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.ui.velocity.VelocityEngineUtils;

public class SendMail {

protected static final Log log = LogFactory.getLog(SendMail.class);

private MailSender mailSender;

private SimpleMailMessage message;

private VelocityEngine velocityEngine;

public void setVelocityEngine(VelocityEngine velocityEngine) {

this.velocityEngine = velocityEngine;

}

public void setMailSender(MailSender mailSender) {

this.mailSender = mailSender;

}

public void setMessage(SimpleMailMessage message) {

this.message = message;

}

public void sendEmail(Map model) {

message.setTo("jfishsz@163.com");

message.setSubject("subject");

String result = null;

try {

result = VelocityEngineUtils.mergeTemplateIntoString(

velocityEngine, "accountCreated.vm", model);

} catch (VelocityException e) {

e.printStackTrace();

}

message.setText(result);

mailSender.send(message);

}

}

测试类:SendMailTest.java

package net.pms.email;

import java.util.HashMap;

import java.util.Map;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SendMailTest extends TestCase {

String[] paths = { "config/applicationContext*.xml" };

ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);

SendMail s = (SendMail) ctx.getBean("sendMail");

protected void setUp() throws Exception {

super.setUp();

}

protected void tearDown() throws Exception {

super.tearDown();

}

/*

* Test method for 'net.pms.email.SendMail.sendEmail(Map)'

*/

public void testSendEmail() {

Map model = new HashMap();

model.put("message", "msg");

model.put("username", "jack");

model.put("Password", "666666");

model.put("applicationURL", "www.163.com");

s.sendEmail(model);

}

}

posted on 2006-03-13 10:35 黑咖啡 阅读(1666) 评论(1)  编辑  收藏 所属分类: Velocity

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值