利用 spring+freemarker 發送郵件

最近公司要用到給註冊會員發送郵件的功能,這裏採用spring+freemarker模板來發送郵件,其中模板能夠自定義,欲瞭解freemarker請看我寫的利用freemarker 靜態化網頁,裏面介紹的很詳細和怎麼使用。html

在作本次試驗以前須要spring相關的jar和freemarker.jarjava

①定義發送郵件的模板web

demo.ftlspring

[html] view plaincopyprint?spring-mvc

  1. <!--這裏能夠寫html代碼,傳遞過來的參數能夠用${}來接收 ,非常方便-->  mvc

  2.   

  3. <a href="http://www.xxx.com">你好${username}</a>  app

②發送郵件java類ui

[java] view plaincopyprint?this

  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能夠定時帥新模板,這樣就能夠不重啓服務了

相關文章
相關標籤/搜索