freemarker+javaMailSender+springmvc实现邮件发送

applicationContext-app.xml

	<!-- freemarker configuration -->
	<bean id="freemarkerConfiguration"
		class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
		<property name="preferFileSystemAccess" value="false" />
		<property name="freemarkerSettings">
			<props>
				<prop key="localized_lookup">false</prop>
				<prop key="template_update_delay">5</prop>
				<prop key="cache_storage">strong:20, soft:20</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
				<prop key="output_encoding">UTF-8</prop>
			</props>
		</property>
		<property name="templateLoaderPath" value="classpath:/templates/" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>


	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="${mail.host}" />
		<property name="username" value="${mail.username}" />
		<property name="password" value="${mail.password}" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
				<prop key="mail.smtp.socketFactory.fallback">false</prop>
				<prop key="mail.smtp.port">465</prop>
				<prop key="mail.smtp.socketFactory.port">465</prop>
				<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
				<prop key="mail.smtp.timeout">25000</prop>
			</props>
		</property>
	</bean>
	
	<bean id="templateSendEmail" class="com.mvc.mail.service.TemplateSendEmail">     
	    <property name="sender" ref="javaMailSender"></property>     
	    <property name="freemarkerConfiguration" ref="freemarkerConfiguration"></property>     
	</bean>   

其中的value值是从properties配置文件中读取的。

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<property name="locations">
			<list>
				<value>classpath:/application.properties</value>
				<value>classpath:/jdbc.properties</value>
				<value>classpath:/global.properties</value>
			</list>
		</property>
	</bean>

application.properties:

mail.host=smtp.qq.com
mail.username=xxx@qq.com
mail.password=xxx
#the setting for email server.
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false

TemplateSendEmail.java


package com.mvc.mail.service;

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.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.mvc.security.model.User;

import freemarker.template.Configuration;
import freemarker.template.Template;
@Service("templateSendEmail")
public class TemplateSendEmail {
	private static JavaMailSender sender = null;      
    private static Configuration freemarkerConfiguration = null;
    
	public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) {
		this.freemarkerConfiguration = freemarkerConfiguration;
	}

	public void setSender(JavaMailSender sender) {      
        this.sender = sender;      
    }      

	//通过模板构造邮件内容,参数username将替换模板文件中的${username}标签。      
    private String getMailText(User user){      
        String htmlText="";      
        try {      
            //通过指定模板名获取FreeMarker模板实例  
            Template tpl=freemarkerConfiguration.getTemplate("registerUser.ftl");      
            //FreeMarker通过Map传递动态数据      
            Map map=new HashMap();  
            map.put("userName",user.getUserName());   
            map.put("loginName",user.getLoginName());    
            //解析模板并替换动态数据,最终username将替换模板文件中的${username}标签。      
            htmlText=FreeMarkerTemplateUtils.processTemplateIntoString(tpl,map);      
        } catch (Exception e) {      
            e.printStackTrace();      
        }      
        return htmlText;      
    }      
    //发送模板邮件  
    public boolean sendTemplateMail(User user) throws MessagingException{  
        MimeMessage msg=sender.createMimeMessage();  
        MimeMessageHelper helper=new MimeMessageHelper(msg,false,"utf8");//由于是html邮件,不是mulitpart类型  
        helper.setFrom("xxx@qq.com");  
        helper.setTo(user.getLoginName());  
        helper.setSubject("激活你的帐号(来自:五十金)");  
        String htmlText=getMailText(user);//使用模板生成html邮件内容  
        helper.setText(htmlText, true);  
        sender.send(msg);  
        return true;
    }  
    
}

模板

放在之前在xml中配的classpath:/templates中

如图:


模板的内容和一般html一样,只要把你想要动态显示的地方,换成${xxx},这个是在getMailTexxt方法中map.put配置的。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值