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