Spring 发送邮件 (3) Spring使用模板Freemarker

环境

Spring 发送邮件 (3) Spring使用模板Freemarker_第1张图片

 

源码分析

package com.macower.spring.jmail; 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 org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import freemarker.template.Template; import freemarker.template.TemplateException; public class FreeMarkService { private JavaMailSender sender; private FreeMarkerConfigurer freeMarkerConfigurer = null; public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) { this.freeMarkerConfigurer = freeMarkerConfigurer; } /** * 添加模板内容 * @param content * @return */ private String getMailText(String content){ String htmlText = ""; Map map = new HashMap(); map.put("content",content); Template tpl = null; try { tpl = freeMarkerConfigurer.getConfiguration().getTemplate("reg.ftl");//加载资源文件 htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);//加入map到模板中 对应${content} } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } return htmlText; } public void sendTemplate(MailSenderInfo info) throws MessagingException{ MimeMessage msg = sender.createMimeMessage(); // false表示非marltipart,UTF-8为字符编码 MimeMessageHelper helper = new MimeMessageHelper(msg, false, "UTF-8"); helper.setSubject(info.getSubject()); helper.setFrom(info.getFromAddress()); helper.setTo(info.getToAddress()); helper.setText(this.getMailText(info.getContent()), true);// 设置为true表示发送的是HTML sender.send(msg); } public void setSender(JavaMailSender sender) { this.sender = sender; } }

 

reg.ftl文件内容

<html> <head> <meta http-equiv="content-type" content="text/html;chartset=utf-8"; > </head> <body> 尊敬的 用户: 您好欢迎使用本邮件系统! <font size=20 color='red'> 以下是内容: ${content} </font> </body> </html>

 

 

 测试发送邮件

 package com.macower.spring.jmail; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSendMail { public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext( "application*.xml"); FreeMarkService sss = (FreeMarkService) ctx .getBean("freeMarkService"); MailSenderInfo mailInfo = new MailSenderInfo(); // 这个类主要是设置邮件 mailInfo.setMailServerHost("smtp.qq.com");// 服务器地址 mailInfo.setMailServerPort("25");// 端口 mailInfo.setValidate(true); // 是否验证,这个地方是必须的 mailInfo.setFromAddress("[email protected]");// 这个是你的邮箱 // ,在程序发送一方需对应,就是你在配置文件的发送邮箱的名称 mailInfo.setToAddress("[email protected]");// 目的地 mailInfo.setSubject("这是一封模板邮件"); mailInfo.setContent("模板邮件测试内容"); sss.sendTemplate(mailInfo); System.out.println("模板邮件发送完毕"); } }

 

配置文件

 

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com" /> <property name="username" value="[email protected]" /><!-- 这个地方输入的是你的邮件地址 --> <property name="password" value="对应的密码" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- 定义自定义的类进行数据的注入 --> <bean id="freeMarkService" class="com.macower.spring.jmail.FreeMarkService"> <property name="sender" ref="sender" /> <property name="freeMarkerConfigurer" ref="freeMarkerConfigurer" /> </bean> <!-- 配置freeMarkerConfigurer进行属性值的注入 --> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPaths" value="classpath:mailTemplate" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">1800</prop><!-- 模板更新延时 --> <prop key="default_encoding">UTF-8</prop> <prop key="locale">zh_CN</prop> </props> </property> </bean> </beans>

 

 

 

测试结果

 

Spring 发送邮件 (3) Spring使用模板Freemarker_第2张图片

你可能感兴趣的:(spring,freemarker,String,HashMap,Class,encoding)