Spring+Freemarker模板邮件发送
- 在eclipse中新建一个Java Project, 名称为spring-freemarker-mail
2. 在项目下新建一个lib目录,导入下面需要的jar包
commons-logging-1.2.jar
freemarker.jar
mail.jar
servlet-api.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-context-support-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar
3. 新建发送邮件抽象类SendMailBase.java, 内容如下:
packagecn.com.csii.mail;
importjava.util.Date;
importjava.util.Map;
importjavax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
importorg.springframework.mail.javamail.MimeMessageHelper;
/**
* 邮件发送抽象类,该类的主要功能是封装发送邮件的一系列属性,和发送邮件的固定内容,还有一个抽象的生成邮件正文的方法,需要子类去覆盖
* @author liujianguo
*
*/
publicabstractclassSendMailBase {
/**
* 发送邮件主机
*/
privateString host = "localhost";
/**
* 连接到邮件发送服务器端口
*/
privateintport =25;
/**
* 邮件发送协议
*/
privateString protocol = "smtp";
/**
* 账户
*/
privateString username = "aaa";
/**
* 密码
*/
privateString password = "123";
/**
* 发件人邮箱地址
*/
privateString addresser = "[email protected]";
/**
* 收件人邮箱地址
*/
privateString recipient;
/**
* 邮件主题
*/
privateString subject;
/**
* 邮件模板位置
*/
privateString templateLocation;
//此处省略setter、getter方法
/**
* 发送邮件, 在这方法内部会创建一封邮件,然后根据Velocity模板和传入Map模型,合并出邮件内容,并发送出去
* @parammodel
* @throwsException
*/
@SuppressWarnings("rawtypes")
publicvoidsendMail(Map model) throws Exception{
JavaMailSenderImpl sender = newJavaMailSenderImpl();
sender.setHost(host);
sender.setUsername(username);
sender.setPassword(password);
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = newMimeMessageHelper(message, "UTF-8"); //为了解决中文乱码问题
//设置发件人
helper.setFrom(addresser);
//设置收件人
String recipientInModel =(String) model.get("recipient");
if(recipientInModel != null&& !"".equals(recipientInModel.trim())){
helper.setTo(recipientInModel);
}
else{
helper.setTo(recipient);
}
//设置邮件主题
helper.setSubject(subject);
//设置邮件发送时间
helper.setSentDate(newDate());
//设置邮件正文
String text =createMailText(model);
helper.setText(text, true); //如果模板里是html代码, 那么这个一定要设置为true
//发送邮件
sender.send(message);
}
/**
* 生成邮件正文
* @parammodel
* @return
*/
protectedabstractString createMailText(Map model);
}
4.创建根据Velocity模板发送邮件的子类,继承SendMailBase.java
packagecn.com.csii.mail;
importjava.util.Map;
importorg.springframework.ui.freemarker.FreeMarkerTemplateUtils;
importorg.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
importfreemarker.template.Template;
/**
* 使用Freemarker模板发送电子邮件
* @authorAdministrator
*
*/
publicclassSendMailByFreemarkerTemplate extends BaseSendMail {
privateFreeMarkerConfigurer freeMarkerConfigurer;
publicFreeMarkerConfigurer getFreeMarkerConfigurer() {
returnfreeMarkerConfigurer;
}
publicvoidsetFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
@Override
protectedString createMailText(Map model) {
try {
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(getTemplateLocation());
String text =FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
returntext;
} catch(Exception e) {
e.printStackTrace();
}
return"";
}
}
5.新建一个邮件模板(regist-template.ftl)放在src目录下,内容如下:
<html>
<body>
<h3>Hi, ${username}, welcome to the Email</h3>
<div>
now, you are registsuccessfully!
This is freemarker template...
</div>
</body>
</html>
6.配置beans.xml文件, 这个文件也放在src下,内容如下:
<bean id="sendMailByFreemarkerTemplate"class="cn.com.csii.mail.SendMailByFreemarkerTemplate">
<property name="addresser" value="[email protected]"/>
<property name="recipient" value="[email protected]"/>
<property name="templateLocation" value="regist-template.ftl" />
<property name="host" value="localhost"/>
<property name="username" value="aaa"/>
<property name="password" value="123"/>
<property name="subject" value="使用Freemarker模板发送邮件" />
<property name="freeMarkerConfigurer" ref="freeMarkerConfigurer" />
</bean>
<bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="classpath:" />
</bean>
8.创建客户端测试类
package cn.com.csii.mail.client;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.csii.mail.SendMailBase;
public class SendMailClient {
public static voidmain(String[] args) throws Exception {
ApplicationContextcontext = new ClassPathXmlApplicationContext("beans.xml");
SendMailBase mailSend =(SendMailBase) context.getBean("sendMailByFreemarkerTemplate");
Map model = newHashMap();
model.put("username","哈喽哈罗");
mailSend.sendMail(model);
}
}
9.运行客户端程序,结果能正常收到邮件