1.把freemarker.jar架包添加到项目中
2.创建模板文件,以*.ftl(后缀名是ftl)命名,模板示例如下
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html;charset=utf-8"> 4 </head> 5 <body> 6 <font color="blue" size="4" face="Arial"> 7 尊敬的${user}: 8 </font> 9 <br/><br/> 10 <font color="blue" size="4" face="Arial"> 11 恭喜您使用系统!请保管您的账号,如果登录时忘记密码 12 可以在网站登录页找回。<br/> 13 <br/><br/>系统管理员 14 </font> 15 </body> 16 </html>
3.发送邮件示例
1 package cn.bd.mail.template; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import javax.mail.MessagingException; 8 import javax.mail.internet.MimeMessage; 9 10 import org.springframework.mail.javamail.JavaMailSender; 11 import org.springframework.mail.javamail.MimeMessageHelper; 12 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; 13 import freemarker.template.Configuration; 14 import freemarker.template.Template; 15 import freemarker.template.TemplateException; 16 17 public class MailService { 18 private JavaMailSender mailSender; 19 private Configuration freeMarkerConfiguration; 20 public void setMailSender(JavaMailSender mailSender) { 21 this.mailSender = mailSender; 22 } 23 public void setFreeMarkerConfiguration(Configuration freeMarkerConfiguration) { 24 this.freeMarkerConfiguration = freeMarkerConfiguration; 25 } 26 27 private String getMailText(){ 28 String htmlText=""; 29 try{ 30 //获取模板实例 31 Template template=freeMarkerConfiguration.getTemplate("mail.ftl");//mail.ftl就是模板文件 32 //通过Map传递动态数据 33 Map map=new HashMap(); 34 map.put("user", "测试"); 35 //解析模板文件 36 htmlText=FreeMarkerTemplateUtils.processTemplateIntoString(template, map); 37 }catch(IOException e){ 38 e.printStackTrace(); 39 }catch(TemplateException e){ 40 e.printStackTrace(); 41 }catch(Exception e){ 42 e.printStackTrace(); 43 } 44 return htmlText; 45 } 46 47 public void sendMail() throws MessagingException{ 48 MimeMessage mimeMessage=mailSender.createMimeMessage(); 49 MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true,"UTF-8"); 50 helper.setFrom("[email protected]");//发送人 51 helper.setTo("1875[email protected]");//邮件接收人 52 helper.setSubject("标题...."); 53 helper.setText(getMailText(),true);//设置HTML格式邮件内容 54 mailSender.send(mimeMessage); 55 } 56 57 }
4.Spring容器的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 8 9 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 10 <property name="host" value="smtp.163.com"></property> <!-- 服务器名 --> 11 <property name="port" value="25"></property> <!-- 端口号 --> 12 <property name="username" value="13599057417"></property><!-- 用户名 --> 13 <property name="password" value="luofuxiang"></property><!-- 密码 --> 14 <property name="protocol" value="smtp"></property> <!-- 协议 --> 15 <property name="defaultEncoding" value="utf-8"></property> 16 <property name="javaMailProperties"> 17 <props> 18 <prop key="mail.smtp.auth">true</prop> 19 </props> 20 </property> 21 </bean> 22 <bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 23 <property name="templateLoaderPath" value="/cn/bd/mail/template"></property> <!-- 指定模板(mail.ftl)路径 --> 24 <!-- 设置FreeMarker环境变量 --> 25 <property name="freemarkerSettings"> 26 <props> 27 <prop key="default_encoding">UTF-8</prop> 28 </props> 29 </property> 30 </bean> 31 <!-- 配置bean:MailService --> 32 <bean id="mailService" class="cn.bd.mail.template.MailService"> 33 <property name="mailSender" ref="mailSender"></property><!-- 为属性注入值 --> 34 <property name="freeMarkerConfiguration" ref="freeMarkerConfiguration"></property><!-- 给属性注入值 --> 35 </bean> 36 </beans>
时间: 2024-11-08 21:08:09