利用 spring+freemarker 发送邮件

来源:互联网 发布:蚂蚁网络兼职 编辑:程序博客网 时间:2024/06/16 07:57

最近公司要用到给注册会员发送邮件的功能,这里采用spring+freemarker模板来发送邮件,其中模板可以自定义,欲了解freemarker请看我写的利用freemarker 静态化网页,里面介绍的很详细和怎么使用。

在做本次试验之前需要spring相关的jar和freemarker.jar

①定义发送邮件的模板

demo.ftl

[html] view plaincopyprint?
  1. <!--这里可以写html代码,传递过来的参数可以用${}来接收 ,很是方便-->  
  2.   
  3. <a href="http://www.xxx.com">你好${username}</a>  

②发送邮件java类

[java] view plaincopyprint?
  1. package com.woaika.loan.utils;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import javax.mail.MessagingException;  
  6. import javax.mail.internet.MimeMessage;  
  7.   
  8. import org.springframework.mail.MailException;  
  9. import org.springframework.mail.javamail.JavaMailSender;  
  10. import org.springframework.mail.javamail.MimeMessageHelper;  
  11. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;  
  12. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  13.   
  14. import freemarker.template.Template;  
  15.   
  16. /** 
  17.  * 发送邮件 可以自己编写html模板 
  18.  * @author ajun 
  19.  * @email zhaojun2066@gmail.com 
  20.  * @blog http://blog.csdn.net/ajun_studio 
  21.  * 2011-12-6 下午04:49:01 
  22.  */  
  23. public class TemplateEmail {  
  24.   
  25.     private JavaMailSender sender;    
  26.     private FreeMarkerConfigurer freeMarkerConfigurer=null;//FreeMarker的技术类    
  27.         
  28.     public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {    
  29.         this.freeMarkerConfigurer = freeMarkerConfigurer;    
  30.     }    
  31.         
  32.     public void setSender(JavaMailSender sender) {    
  33.         this.sender = sender;    
  34.     }    
  35.       
  36.     /** 
  37.      * 生成html模板字符串 
  38.      * @param root 存储动态数据的map 
  39.      * @return 
  40.      */  
  41.     private String getMailText(Map<String,Object> root,String templateName){  
  42.          String htmlText="";    
  43.             try {    
  44.                 //通过指定模板名获取FreeMarker模板实例    
  45.                 Template tpl=freeMarkerConfigurer.getConfiguration().getTemplate(templateName);    
  46.                 htmlText=FreeMarkerTemplateUtils.processTemplateIntoString(tpl,root);    
  47.             } catch (Exception e) {    
  48.                 e.printStackTrace();    
  49.             }    
  50.             return htmlText;    
  51.     }  
  52.       
  53.     /** 
  54.      * 发送邮件 
  55.      * @param root 存储动态数据的map 
  56.      * @param toEmail 邮件地址 
  57.      * @param subject 邮件主题 
  58.      * @return 
  59.      */  
  60.     public boolean sendTemplateMail(Map<String,Object> root,String toEmail,String subject,String templateName){    
  61.         try {  
  62.             MimeMessage msg=sender.createMimeMessage();    
  63.             MimeMessageHelper helper=new MimeMessageHelper(msg,false,"utf-8");//由于是html邮件,不是mulitpart类型    
  64.             helper.setFrom("admin@xxx.com");    
  65.             helper.setTo(toEmail);    
  66.             helper.setSubject(subject);    
  67.             String htmlText=getMailText(root,templateName);//使用模板生成html邮件内容    
  68.             helper.setText(htmlText, true);    
  69.             sender.send(msg);  
  70.             //System.out.println("成功发送模板邮件");    
  71.             return true;  
  72.         } catch (MailException e) {  
  73.            // System.out.println("失败发送模板邮件");   
  74.             e.printStackTrace();  
  75.             return false;  
  76.         } catch (MessagingException e) {  
  77.         //  System.out.println("失败发送模板邮件");   
  78.             e.printStackTrace();  
  79.             return false;  
  80.         }    
  81.          
  82.     }    
  83. }  


③实例化相关freemarker类的配置文件,并交给spring来管理

applicationContext-sendMail.xml


[html] view plaincopyprint?
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.  xmlns:context="http://www.springframework.org/schema/context"  
  3.  xmlns:p="http://www.springframework.org/schema/p"  
  4.  xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.  xmlns:aop="http://www.springframework.org/schema/aop"  
  7.  xmlns:tx="http://www.springframework.org/schema/tx"  
  8.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.       http://www.springframework.org/schema/context  
  11.       http://www.springframework.org/schema/context/spring-context.xsd  
  12.       http://www.springframework.org/schema/tx   
  13.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  14.       http://www.springframework.org/schema/aop  
  15.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  16.       http://www.springframework.org/schema/mvc  
  17.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  18.              
  19.     <bean id="freeMarker" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  20.       <property name="templateLoaderPath" value="classpath:mailtemplates"/><!--指定模板文件目录-->   
  21.       <property name="freemarkerSettings"><!-- 设置FreeMarker环境属性-->   
  22.           <props>  
  23.               <prop key="template_update_delay">1800</prop><!--刷新模板的周期,单位为秒-->   
  24.               <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->  
  25.               <prop key="locale">zh_CN</prop><!-- 本地化设置-->  
  26.           </props>  
  27.       </property>  
  28.     </bean>  
  29.     <bean id="templateEmail" class="com.woaika.loan.utils.TemplateEmail">  
  30.         <property name="sender" ref="mailsender"></property>  
  31.         <property name="freeMarkerConfigurer" ref="freeMarker"></property>  
  32.     </bean>   
  33.       
  34.     <bean id="mailsender"    
  35.         class="org.springframework.mail.javamail.JavaMailSenderImpl">    
  36.         <property name="host">    
  37.             <value>mail.xx.com</value>    
  38.         </property>    
  39.         <property name="javaMailProperties">    
  40.             <props>    
  41.                 <prop key="mail.smtp.auth">true</prop>    
  42.                 <prop key="mail.smtp.timeout">25000</prop>    
  43.             </props>    
  44.         </property>    
  45.         <property name="username">    
  46.             <value>admin</value>    
  47.         </property>    
  48.         <property name="password">    
  49.             <value>qq22qq56we</value>    
  50.         </property>    
  51.     </bean>    
  52.       
  53. </beans>  

④调用的时候,只需调用的类中注入TemplateEmail这个,然后调用相关的方法就可以了


[java] view plaincopyprint?
  1. package com.woaika.loan.front.apply.action;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.springframework.stereotype.Component;  
  9.   
  10. import com.woaika.loan.utils.TemplateEmail;  
  11.   
  12. /** 
  13.  * 发送邮件 
  14.  * @author ajun 
  15.  * @email zhaojun2066@gmail.com 
  16.  * @blog http://blog.csdn.net/ajun_studio 
  17.  * 2012-3-13 上午10:50:26 
  18.  */  
  19. @Component("demoEmail")  
  20. public class DemoEmail {  
  21.   
  22.     private TemplateEmail templateEmail;  
  23.       
  24.     @Resource(name="templateEmail")  
  25.     public void setTemplateEmail(TemplateEmail templateEmail) {  
  26.         this.templateEmail = templateEmail;  
  27.     }   
  28.       
  29.     public void send(){  
  30.         Map<String,Object> root = new HashMap<String,Object>();  
  31.         root.put("username""ajun");  
  32.         templateEmail.sendTemplateMail(root, "ajun@gmail.com","主题标题","demo.ftl");  
  33.     }  
  34.       
  35.       
  36.       
  37. }  

ok ,赶快试试吧 很方便,而且spring可以定时帅新模板,这样就可以不重启服务了
0 0