posts - 78,  comments - 48,  trackbacks - 0
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);
  }

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

FeedBack:
# re: spring+velocity自动发送邮件
2009-11-06 10:58 | geek87
相当好,谢谢。。。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

留言簿(2)

随笔分类(67)

文章分类(43)

Good Article

Good Blogs

Open Source

最新随笔

最新评论

阅读排行榜

评论排行榜