浏览 2080 次
锁定老帖子 主题:spring+velocity自动发送邮件
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-02-09
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- For mail settings and future properties files --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mail.properties</value> </list> </property> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>${mail.host}</value> </property> <property name="username"> <value>${mail.username}</value> </property> <property name="password"> <value>${mail.password}</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.smtp.timeout"> ${mail.smtp.timeout} </prop> </props> </property> </bean> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" singleton="false"> <property name="from"> <value>${mail.default.from}</value> </property> </bean> <bean id="sendMail" class="net.pms.email.SendMail"> <property name="mailSender" ref="mailSender" /> <property name="message" ref="mailMessage" /> <property name="velocityEngine" ref="velocityEngine" /> </bean> <!-- Configure Velocity for sending e-mail --> <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"></prop> </props> </property> </bean> </beans> 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); } } 这只是一个简单的实现介绍,我想通过这个例子进入通往javamail的大门。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |