参考网页: http://uule.iteye.com/blog/1094365
1.在applicationContext.xml的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="freeMarker" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/mailTemplate/"/><!--指定模板文件目录-->
<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性-->
<props>
<prop key="template_update_delay">1800</prop><!--刷新模板的周期,单位为秒-->
<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
<prop key="locale">zh_CN</prop><!-- 本地化设置-->
</props>
</property>
</bean>
<bean id="springSendEmail" class="me.tbs.jxguorong.core.utils.SpringSendEmail">
<property name="sender" ref="mailsender"></property>
<property name="freeMarkerConfigurer" ref="freeMarker"></property>
</bean>
<!-- 发送邮件 -->
<bean id="mailsender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.exmail.qq.com" />
<property name="port" value="25" />
<property name="username" value="968906@jxguorong.com" /> <!--发送方的邮箱账号-->
<property name="password" value="jxgr654321" /> <!--发送方的邮箱密码-->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
</beans>
2.java代码
private JavaMailSender sender;
private FreeMarkerConfigurer freeMarkerConfigurer=null;//FreeMarker的技术类
public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
public String sendOneTemplateMail(String toAddress,String userName,String pwd) throws MessagingException{
String res = "0";
MimeMessage msg=sender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,false,"utf8");//由于是html邮件,不是mulitpart类型
helper.setFrom("info@968906.com"); //发送方的邮箱
helper.setSubject("XXXX-密码重置"); //邮件标题
String htmlText="尊敬的用户:(" + userName+ ") 您的密码已重置为:"+pwd +"; 请登录会员中心修改新密码! xxxx"; //发送给对方的内容
helper.setText(htmlText, true);
try {
helper.setTo(toAddress);//对方邮箱
try{
sender.send(msg);
System.out.println("成功发送模板邮件:to"+toAddress);
} catch (MailSendException e) {
res = "2";
System.out.println("成功发送模板邮件:"+toAddress+"失败,地址不对");
}
} catch (Exception e) {
e.printStackTrace();
res="1";
}
return res;
}
}
//发送模板邮件 (这个是发送给对方的固定内容,可以不写。)
public void sendTemplateMail(String username) throws MessagingException{
MimeMessage msg=sender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(msg,false,"utf8");//由于是html邮件,不是mulitpart类型
helper.setFrom("dongsanbo@sina.com");
helper.setTo("dongsanbo@sina.com");
helper.setSubject("注册成功-模板邮件");
String htmlText=getMailText(username);//使用模板生成html邮件内容
helper.setText(htmlText, true);
sender.send(msg);
System.out.println("成功发送模板邮件");
}
public static void main(String[] args) throws MessagingException {
// TODO Auto-generated method stub
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
TemplateEmailService ms=(TemplateEmailService) ctx.getBean("templateEmail");
//发送模板邮件
ms.sendTemplateMail("yefriend");
}//end method
}