最近公司要用到給註冊會員發送郵件的功能,這裏採用spring+freemarker模板來發送郵件,其中模板能夠自定義,欲瞭解freemarker請看我寫的利用freemarker 靜態化網頁,裏面介紹的很詳細和怎麼使用。html
在作本次試驗以前須要spring相關的jar和freemarker.jarjava
①定義發送郵件的模板web
demo.ftlspring
[html] view plaincopyprint?spring-mvc
<!--這裏能夠寫html代碼,傳遞過來的參數能夠用${}來接收 ,非常方便--> mvc
<a href="http://www.xxx.com">你好${username}</a> app
②發送郵件java類ui
[java] view plaincopyprint?this
package com.woaika.loan.utils; 編碼
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Template;
/**
* 發送郵件 能夠本身編寫html模板
* @author ajun
* @email zhaojun2066@gmail.com
* @blog http://blog.csdn.net/ajun_studio
* 2011-12-6 下午04:49:01
*/
public class TemplateEmail {
private JavaMailSender sender;
private FreeMarkerConfigurer freeMarkerConfigurer=null;//FreeMarker的技術類
public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
/**
* 生成html模板字符串
* @param root 存儲動態數據的map
* @return
*/
private String getMailText(Map<String,Object> root,String templateName){
String htmlText="";
try {
//經過指定模板名獲取FreeMarker模板實例
Template tpl=freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
htmlText=FreeMarkerTemplateUtils.processTemplateIntoString(tpl,root);
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
/**
* 發送郵件
* @param root 存儲動態數據的map
* @param toEmail 郵件地址
* @param subject 郵件主題
* @return
*/
public boolean sendTemplateMail(Map<String,Object> root,String toEmail,String subject,String templateName){
try {
MimeMessage msg=sender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,false,"utf-8");//因爲是html郵件,不是mulitpart類型
helper.setFrom("admin@xxx.com");
helper.setTo(toEmail);
helper.setSubject(subject);
String htmlText=getMailText(root,templateName);//使用模板生成html郵件內容
helper.setText(htmlText, true);
sender.send(msg);
//System.out.println("成功發送模板郵件");
return true;
} catch (MailException e) {
// System.out.println("失敗發送模板郵件");
e.printStackTrace();
return false;
} catch (MessagingException e) {
// System.out.println("失敗發送模板郵件");
e.printStackTrace();
return false;
}
}
}
③實例化相關freemarker類的配置文件,並交給spring來管理
applicationContext-sendMail.xml
[html] view plaincopyprint?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="freeMarker" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="classpath:mailtemplates"/><!--指定模板文件目錄-->
<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>
<bean id="templateEmail" class="com.woaika.loan.utils.TemplateEmail">
<property name="sender" ref="mailsender"></property>
<property name="freeMarkerConfigurer" ref="freeMarker"></property>
</bean>
<bean id="mailsender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>mail.xx.com</value>
</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>admin</value>
</property>
<property name="password">
<value>qq22qq56we</value>
</property>
</bean>
</beans>
④調用的時候,只需調用的類中注入TemplateEmail這個,而後調用相關的方法就能夠了
[java] view plaincopyprint?
package com.woaika.loan.front.apply.action;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.woaika.loan.utils.TemplateEmail;
/**
* 發送郵件
* @author ajun
* @email zhaojun2066@gmail.com
* @blog http://blog.csdn.net/ajun_studio
* 2012-3-13 上午10:50:26
*/
@Component("demoEmail")
public class DemoEmail {
private TemplateEmail templateEmail;
@Resource(name="templateEmail")
public void setTemplateEmail(TemplateEmail templateEmail) {
this.templateEmail = templateEmail;
}
public void send(){
Map<String,Object> root = new HashMap<String,Object>();
root.put("username", "ajun");
templateEmail.sendTemplateMail(root, "ajun@gmail.com","主題標題","demo.ftl");
}
}
ok ,趕快試試吧 很方便,並且spring能夠定時帥新模板,這樣就能夠不重啓服務了