`

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
<?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的大门。
分享到:
评论

相关推荐

    spring4.1核心包

    包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 18. spring-webmvc-4.1.1.RELEASE.jar 包含...

    spring in action英文版

     7.2 发送电子邮件  7.3 调度任务  7.3.1 使用Java Timer调度任务  7.3.2 使用Quartz调度器  7.3.3 按调度计划调用方法  7.4 使用JMS发送消息  7.4.1 使用JMS模板发送消息  7.4.2 消费消息 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Spring 2.0 开发参考手册

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    Spring中文帮助文档

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack Thierry Templier Erwin ...

    Spring API

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    Spring in Action(第2版)中文版

    4.3.1为spring切面创建自动代理 4.3.2自动代理@aspectj切面 4.4定义纯粹的pojo切面 4.5注入aspectj切面 4.6小结 第二部分企业spring 第5章使用数据库 5.1spring的数据访问哲学 5.1.1了解spring数据访问的...

    Spring in Action(第二版 中文高清版).part2

    4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...

    Spring in Action(第二版 中文高清版).part1

    4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...

    Spring面试题

    ☆ 电子邮件服务,向用户发送有关信用卡状态的电子邮件。 三个接口 对于这个示例,我假设服务已经存在,理想的情况是用松散耦合的方式把它们集成在一起。以下清单显示了三个服务的应用程序接口。 清单 3. ...

    springboot参考指南

    发送邮件 xi. 32. 使用JTA处理分布式事务 i. 32.1. 使用一个Atomikos事务管理器 ii. 32.2. 使用一个Bitronix事务管理器 iii. 32.3. 使用一个J2EE管理的事务管理器 iv. 32.4. 混合XA和non-XA的JMS连接 v. 32.5. 支持...

Global site tag (gtag.js) - Google Analytics