在网站注册时,我们有时候会需要进行激活。以发送邮件的形式来进行帐号激活。
对应的测试类:
注册模版:registerUsr.ftl
freemark的配置:
下面我们用spring的mail进行发送邮件。
配置如下:
<bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.163.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>username@163.com</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<bean id="abstractEmail" class="com.ysw.framework.util.AbstractEmail" abstract="true">
<property name="sender" ref="sender"></property>
<property name="freeMarkerConfigurer" ref="freeMarkerConfigurer"></property>
</bean>
<bean id="emailService" class="com.ysw.framework.util.EmailService" parent="abstractEmail">
</bean>
对应的java类:
public abstract class AbstractEmail {
protected String from;
protected String subject;
protected JavaMailSender sender;
protected FreeMarkerConfigurer freeMarkerConfigurer = null; // FreeMarker的技术类
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public JavaMailSender getSender() {
return sender;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
return freeMarkerConfigurer;
}
public void setFreeMarkerConfigurer(
FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
/**
* 发送单个html格式邮件
* @param content 用户注册的id,昵称(nickName),激活地址(url)
* @param address 发送地址 发送的邮箱地址
* @throws Exception
*/
public abstract void sendEmail(Map<?, ?> content, String address) throws Exception;
/**
* 批量发送html格式邮件
*/
public abstract void sendBatchEmail(String content, List<String> address) throws Exception;
}
public class EmailService extends AbstractEmail {
@Override
public void sendEmail(Map<?, ?> content, String address) throws Exception {
MimeMessage msg = sender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码,true表示为multipart邮件
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
helper.setTo(address); // 邮件接收地址
helper.setFrom(from); // 邮件发送地址,这里必须和xml里的邮件地址相同一致
helper.setSubject(subject); // 主题
String htmlText = getMailText(content); // 使用模板生成html邮件内容
helper.setText(htmlText, true); // 邮件内容,注意加参数true,表示启用html格式
sender.send(msg); // 发送邮件
}
@Override
public void sendBatchEmail(String content, List<String> address) {
}
/**
* 集合转换字符串
*/
public String getMailList(List<String> to) {
StringBuffer toList = new StringBuffer();
int length = to.size();
if (to != null && length < 2) {
toList.append(to.get(0));
} else {
for (int i = 0; i < length; i++) {
toList.append(to.get(i));
if (i != (length - 1)) {
toList.append(",");
}
}
}
return toList.toString();
}
// 通过模板构造邮件内容,参数content将替换模板文件中的${content}标签。
private String getMailText(Map<?, ?> map) throws Exception {
String htmlText = "";
// 通过指定模板名获取FreeMarker模板实例
Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(
"registerUser.ftl");
// FreeMarker通过Map传递动态数据
// 注意动态数据的key和模板标签中指定的属性相匹配
// 解析模板并替换动态数据,最终content将替换模板文件中的${content}标签。
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);
return htmlText;
}
}
对应的测试类:
@Test
public void sendMailTest() throws Exception{
EmailService es = (EmailService) ac.getBean("emailService");
es.setSubject("注册");
es.setFrom("username@163.com");
Map<String, Object> content = new HashMap<String, Object>();
String id = "123456";
content.put("id", id);
content.put("nickName", "王尼玛");
String url = "http://192.168.1.16:8080/customer/registerCheck.html?id="+id;
content.put("url", url);
String address = "to@163.com";
es.sendEmail(content, address);
}
注册模版:registerUsr.ftl
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf8">
</head>
<body>
亲爱的<font color='red' size='30'>${nickName}</font>: <br/>
请点击下面的地址进行激活:<a href="${url}">${url}</a>
</body>
</html>
freemark的配置:
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
</props>
</property>
</bean>
有图有真相: