`
chaoyi
  • 浏览: 291073 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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());
		}
	}
}

 

效果图:

 

 

 

 

  • 大小: 25.5 KB
  • 大小: 75.2 KB
  • lib.zip (900.2 KB)
  • 下载次数: 1
分享到:
评论

相关推荐

    spring整合freemarker发送邮件例子

    spring整合freemarker实现发送邮件, html中内嵌图片,添加附件, 解决乱码问题.

    Spring Boot整合邮件发送并保存历史发送邮箱

    邮件发送支持以 HTML 的形式去构建我们喜欢的文本格式,Spring 对 HTML 格式的邮件也做出了支持,非常方便使用。 我们在 MailService 中添加支持 HTML 发送的方法. Spring Boot整合邮件发送并保存历史发送邮箱 和...

    基于springboot+MyBatis实现的某房产平台系统源码+项目说明(毕设).zip

    Spring Mail + Spring Task完成异步发送激活链接,邮件发送,验证 Jquery BootStrap Ajax springBoot Guava Cache(java工具类集的基础库,注册key的绑定) Druid(监控目的的数据库连接池),配置文件里面...

    J2EE spring mvc mybatis bootstrap HTML5 后台框架 控制台 mysql版本_spring3.0

    群发邮件,可以发html、纯文本格式,可以发给任意邮箱(实现批量发送广告邮件) 5. 群发or单独 发送短信,支持两种第三方短信商接口 6. spring aop 事物处理 7. 代码生成器 (freemarker), 代码 zip 压缩打包 8. MD5...

    spring4.1核心包

    可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。 外部依赖spring-beans, (spring-aop)。 5. spring-context-support-4.1.1....

    基于Java Web(springboot)实现房产平台 【100011241】

    Spring Mail + Spring Task完成异步发送激活链接,邮件发送,验证;Druid(监控目的的数据库连接池),配置文件里面加密数据库密码;Freemarker:基于Java的生成html的模板引擎,用来前后端交互等等。 项目功能目标 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    spring in action英文版

     1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么  1.3 开始Spring之旅  1.4 理解反向控制  1.4.1 依赖注入  1.4.2 IoC应用  1.4.3 企业级应用中的...

    Spring 2.0 开发参考手册

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    J2EE spring mvc mybatis bootstrap HTML5 后台框架 控制台 oracle版本_spring3.0

    群发邮件,可以发html、纯文本格式,可以发给任意邮箱(实现批量发送广告邮件) 5. 群发or单独 发送短信,支持两种第三方短信商接口 6. spring aop 事物处理 7. 代码生成器 (freemarker), 代码 zip 压缩打包 8. MD5...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

Global site tag (gtag.js) - Google Analytics