spring applicationContext.xml 配置:
<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="hello@163.com" />
<property name="password" value="hello123" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="WEB-INF/"></property>
<!--
<property name="velocityProperties">
<value>resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
</property>
-->
</bean>
registration-confirmation 文件:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="GBK">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h5>
尊敬的会员:
</h5>
<h5>
您的激活码已审核通过,于系统时间${user.lastLoginDate}生成邀请。
</h5>
<h5>
您的激活码:${user.userId},请您通过下面的链接,完成您的帐户注册激活:
</h5>
<a href="http://hello.com/forward.do?id=${user.userId}">http://www.hello.com/?name=${user.userName}&id=${user.userId}</a>
<h5>
系统将自动跟踪并激活您的帐户。
</h5>
<h5>
感谢您的关注和支持!
</h5>
--------------------------------------------------------------------
<h5>
请勿直接回复本邮件,如有任何疑问,联系网站客服人员
</h5>
</body>
</html>
Java中实现代码:
public boolean sendEmail(final User user) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,"UTF-8");
message.setSubject("Hello您好(中国)网络科技有限公司用户注册验证");
message.setTo(user.getEmail());
message.setFrom("hello@163.com");
Map model = new HashMap();
model.put("user", user);
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "registration-confirmation.vm","UTF-8", model);
message.setText(text, true);
}
};
try{
this.mailSender.send(preparator);
return true;
}catch(MailException e) {
e.printStackTrace();
return false;
}
}