Spring+Freemarker 使用163发HTML格式的邮件

图示:

 

mail.ftl HTML模板

<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
	<font color='blue' size='5' face='Arial'>
	        尊敬的${user}您好:
	</font>
	<br/><br/>
	<font color='black' size='4' face='Arial'>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;恭喜您在员工社区注册账号成功!
		请妥善保管您的账号,如果登录时忘记密码,可以在网站登录页找回。<br/>
		<br/><br/>系统管理员
	</font>
</body>
</html>

 

MailService 发送业务类

package cn.mail;
import java.io.IOException;
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.ui.freemarker.FreeMarkerTemplateUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
 * 发送 FreeMarker 模板邮件
 * */
public class MailService {
	private JavaMailSender mailSender;//必须使用 JavaMailSende,是  MailSender 子接口
	private Configuration freeMarkerConfiguration;//注入 FreeMarker 配置
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	public void setFreeMarkerConfiguration(Configuration freeMarkerConfiguration) {
		this.freeMarkerConfiguration = freeMarkerConfiguration;
	}
	//通过模板得到邮件的内容
	private String getMailText(){
		String htmlText = "";
		try {
			//获取模板实例
			Template template =freeMarkerConfiguration.getTemplate("mail.ftl");
			//通过 Map 传递动态数据
			Map<String, String> map = new HashMap<String, String>();
			map.put("user", "张三");
			//解析模板文件
			htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return htmlText;
	}
	/**
	 * 发邮件方法
	 * */
	public void sendMail() throws MessagingException,IOException{
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
		helper.setFrom("chaoyi77@163.com");
		helper.setTo("ichaoyv@163.com");
		helper.setSubject("欢迎来到员工社区");
		helper.setText(getMailText(),true);
		mailSender.send(mimeMessage);
	}
}

 

applicationContext.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<!-- Spring 实现的发邮件的类 -->
	<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="chaoyi77@163.com" /><!-- 用户名 -->
		<property name="password" value="******" /><!-- 密码 -->
		<property name="protocol" value="smtp" /><!-- 协议 -->
		<property name="defaultEncoding" value="utf-8" /><!-- 默认编码 -->
		<property name="javaMailProperties">
			<props>
				<!-- 设置 SMT 服务器需要用户验证  -->
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>		
	</bean>
	<!-- 配置 FreeMarker -->
	<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
		<!-- 指定模板文件路径 -->
		<property name="templateLoaderPath" value="cn/mailtemplate/"></property>
		<!-- 设置 FreeMarker 环境变量 -->
		<property name="freemarkerSettings">
			<props>
				<prop key="default_encoding">utf-8</prop>
			</props>
		</property>
	</bean>
	<!-- 发邮件的类 -->
	<bean id="mailService" class="cn.mail.MailService">
		<property name="mailSender" ref="mailSender" />
		<!-- FreeMarker 配置类 -->
		<property name="freeMarkerConfiguration" ref="freeMarkerConfiguration"></property>
	</bean>
</beans>

 

MailTest 测试类

package cn.mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MailTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		try {
			MailService mailService = (MailService) context.getBean("mailService");
			mailService.sendMail();
			System.out.println("发送成功");
		} catch (Exception e) {
			System.out.println("发送失败");
			System.out.println(e.toString());
		}
	}
}

 

效果图:

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值