据说Spring Mail比Java Mail封装的更好,用起来更方便一点,所以在项目里觉得采用Spring Mail来实现发送邮件的功能。Spring Mail配置起来果然挺简单的,很快发送邮件就成功了。下面是需要配置的一些代码:
aplicationContext.xml:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">250000</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.port">587</prop>
<prop key="mail.smtp.socketfactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketfactory.fallback">false</prop>
<prop key="mail.smtp.port">587</prop>
</props>
</property>
<property name="username" value="你的邮件名@gmail.com"></property>
<property name="password" value="你的密码"></property>
</bean>
<bean id="freeMaker"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="mailTemplate"></property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">1800</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
在webroot下有个mailTemplate文件夹下存放邮件的模板,贴出来一个给大家看看:
registerUser.ftl:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>发送激活邮件</title>
</head>
<body>
<div class="bgi_remain">
<ul>
<li class="friends_email">
<p>亲爱的 <span class="txt_greeen">${name}:</span></p>
<p>请点击下面的链接激活账号完成注册。<span style="font-size:12px; color:#f00">(请在48小时内完成)</span><br/>
<a href="http://www.uuce.com/userRegister/activateUser.eri?email=${email}&activationCode=${activationCode}">
http://www.uuce.com/userRegister/activateUser.eri?email=${email}&activationCode=${activationCode}
</a></p>
<p>如果该链接无法点击,请直接拷贝以上链接到浏览器地址栏中访问。</p>
</li>
</ul>
</div>
<div class="bgi_regmain_bottom"></div>
</body>
</html>
最后配置一下Action就可以了,我是把它写成了一个Service,SendActivationEmailServiceImpl.java:
package com.uuce.register.service.impl;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import com.uuce.register.service.SendActivationEmailService;
import freemarker.template.Template;
@Component
public class SendActivationEmailServiceImpl implements
SendActivationEmailService {
@Autowired
private JavaMailSender sender;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public void setFreeMarkerConfigurer(
FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
/**
* 套用邮件模板
*/
@SuppressWarnings("unchecked")
private String getMailText(String email, String name, String activationCode) {
String htmlText = "";
try {
Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(
"registerUser.ftl");
Map map = new HashMap();
map.put("email", email);
map.put("name", name);
map.put("activationCode", activationCode);
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl,
map);
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
/*
* 使用SpringMail发送邮件
*/
public void sendActivationEmail(String email, String name,
String activationCode) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true,
"utf-8");
helper.setSubject("恭喜您成功注册为会员!请激活账号");
helper.setFrom("UUCE<你的邮箱名@gmail.com>");
helper.setTo(email);
String htmlText = getMailText(email, name, activationCode);
helper.setText(htmlText, true);
System.out.println("Ready to send the email.");
try {
sender.send(message);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("Email send success.");
}
}
做到这里发送邮件的功能就完成了,检查下jar包是否都导入好了,Spring mail依赖的jar包有:spring.jar,mail.jar,commons-logging.jar,activation.jar。