Spring+Freemarker 使用163发HTML格式的邮件
图示:
mail.ftl HTML模板
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<font color='blue' size='5' face='Arial'>
尊敬的${user}您好:
</font>
<br/><br/>
<font color='black' size='4' face='Arial'>
恭喜您在员工社区注册账号成功!
请妥善保管您的账号,如果登录时忘记密码,可以在网站登录页找回。<br/>
<br/><br/>系统管理员
</font>
</body>
</html>
MailService 发送业务类
package cn.mail;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* 发送 FreeMarker 模板邮件
* */
public class MailService {
private JavaMailSender mailSender;//必须使用 JavaMailSende,是 MailSender 子接口
private Configuration freeMarkerConfiguration;//注入 FreeMarker 配置
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void setFreeMarkerConfiguration(Configuration freeMarkerConfiguration) {
this.freeMarkerConfiguration = freeMarkerConfiguration;
}
//通过模板得到邮件的内容
private String getMailText(){
String htmlText = "";
try {
//获取模板实例
Template template =freeMarkerConfiguration.getTemplate("mail.ftl");
//通过 Map 传递动态数据
Map<String, String> map = new HashMap<String, String>();
map.put("user", "张三");
//解析模板文件
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
/**
* 发邮件方法
* */
public void sendMail() throws MessagingException,IOException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setSubject("欢迎来到员工社区");
helper.setText(getMailText(),true);
mailSender.send(mimeMessage);
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Spring 实现的发邮件的类 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com" /><!-- 服务器 -->
<property name="port" value="25" /><!-- 端口 -->
<property name="username" value="[email protected]" /><!-- 用户名 -->
<property name="password" value="******" /><!-- 密码 -->
<property name="protocol" value="smtp" /><!-- 协议 -->
<property name="defaultEncoding" value="utf-8" /><!-- 默认编码 -->
<property name="javaMailProperties">
<props>
<!-- 设置 SMT 服务器需要用户验证 -->
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!-- 配置 FreeMarker -->
<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<!-- 指定模板文件路径 -->
<property name="templateLoaderPath" value="cn/mailtemplate/"></property>
<!-- 设置 FreeMarker 环境变量 -->
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">utf-8</prop>
</props>
</property>
</bean>
<!-- 发邮件的类 -->
<bean id="mailService" class="cn.mail.MailService">
<property name="mailSender" ref="mailSender" />
<!-- FreeMarker 配置类 -->
<property name="freeMarkerConfiguration" ref="freeMarkerConfiguration"></property>
</bean>
</beans>
MailTest 测试类
package cn.mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MailTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
MailService mailService = (MailService) context.getBean("mailService");
mailService.sendMail();
System.out.println("发送成功");
} catch (Exception e) {
System.out.println("发送失败");
System.out.println(e.toString());
}
}
}
效果图: