DOC

springMailDemo

By Jane Alexander,2014-05-31 19:58
7 views 0
spring mvc demo spring demo spring jdbc demo spring spring mvc spring mail spring mail ssl spring shower spring aop springtomize

     Spring邮件发送实例

     目录

    1.Src/applicationContext.xml配置文件 ........................................................................................... 2 2.com.util.SpringMail.java ................................................................................................................ 3

    3.com/util/xxx.ftl文件的创建 .......................................................................................................... 5 4.需要添加的库文件 ........................................................................................................................ 6

1.Src/applicationContext.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="mailSender"

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

     <!-- 注意这里host一定要是一个能用的smtp服务 -->

     <!-- 经过测试smtp.qq.com的可以用 -->

     <property name="host" value="smtp服务名"></property>

     <property name="javaMailProperties">

     <props>

     <prop key="mail.smtp.auth">true</prop>

     <prop key="mail.smtp.timeout">25000</prop>

     </props>

     </property>

     <!-- 一定要注意不能把这个泄漏出去 -->

     <property name="username" value="帐号" />

     <property name="password" value="密码" />

     </bean>

     <bean id="freeMarker"

     class="org.springframework.web.servlet.view.freemarker.FreeMarker

    Configurer">

     <property name="templateLoaderPath" value="classpath:com/util"

    /><!--指定模板文件目录-->

     <property name="freemarkerSettings"><!-- 设置FreeMarker环境属性-->

     <props>

     <prop key="template_update_delay">1800</prop><!--刷新模板的周期?单位为秒-->

     <prop key="default_encoding">UTF-8</prop><!--模板的编码格 -->

     <prop key="locale">zh_CN</prop><!-- 本地化设置-->

     </props>

     </property>

     </bean>

</beans>

2.com.util.SpringMail.java

package com.util;

import java.io.StringWriter;

    import java.util.HashMap;

    import java.util.Map;

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.mail.SimpleMailMessage;

    import org.springframework.mail.javamail.JavaMailSender;

import freemarker.template.Configuration;

    import freemarker.template.Template;

public class SpringMail {

     private Configuration cfg = new Configuration();

     public static void main(String[] args) throws Exception {

     ApplicationContext ctx = new FileSystemXmlApplicationContext(

     "src/applicationContext.xml");

     JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");

     SpringMail springMail = new SpringMail();

     springMail.sendMail(sender);

     }

     private void sendMail(JavaMailSender sender) throws Exception {

     SimpleMailMessage mail = new SimpleMailMessage();

     mail.setTo("1667647836@qq.com"); //接收人

     mail.setFrom("644509120@qq.com"); //发送人这个要与配置文件中的用户名

    保持一样的帐号

     mail.setSubject("test by amigo");

     //嵌入ftl模版

     cfg.setClassForTemplateLoading(getClass(), "/com/util");

     Map root = new HashMap();

     root.put("username", "sucre"); //模板变量

     Template t = cfg.getTemplate("notify-mail.ftl");

     StringWriter writer = new StringWriter();

     t.process(root, writer);

     //把模版内容写入邮件中

     mail.setText(writer.toString());

     sender.send(mail);

     System.out.println("邮件发送成功?");

     }

}

3.com/util/xxx.ftl文件的创建

    例如,

    欢迎加入!

    亲爱的${username}

    请点击链接完成注册,

    如果您的email程序不支持链接点击?请将上面的地址拷贝至您的浏览器(IE)的地址栏进入****

    您可以在***

    查看……!

    (这是一封自动产生的email?请勿回复。)

4.需要添加的库文件

说明,我这里使用的是Spring2.5的相关包项目采用的是javaee5的需要把javaee.jar包中的mail

activation文件夹删除

Report this document

For any questions or suggestions please email
cust-service@docsford.com