前言:上一篇博文讲解了邮件发送的基础用法(数据是写死的),然而在实际开发中,大多数情况下邮件内容都是根据业务来动态生成的。所以在此篇博文中,我们将讲解邮件发送携带数据的几种方案。

 

一、解析自定义占位符

  实现方法: 通过解析自定义占位符,将传递到邮件中的数据,转换成html内容,进行发送。

1)占位符替换函数

  1. 1   /**
  2. 2 * Replaces place holder ("${}") string in template with values in Map
  3. 3 *
  4. 4 * @param template
  5. 5 * @param models
  6. 6 * @return
  7. 7 */
  8. 8 public static String replacePlaceHolder(String template, Map<String, String> models) {
  9. 9
  10. 10 if (template.indexOf("${") == -1) {
  11. 11 return template;
  12. 12 }
  13. 13
  14. 14 while (true) {
  15. 15
  16. 16 int start = template.indexOf("${");
  17. 17 int end = template.indexOf("}", start);
  18. 18
  19. 19 if (start != -1 && end != -1) {
  20. 20
  21. 21 String key = template.substring(start + 2, end);
  22. 22
  23. 23 if (models.containsKey(key)) {
  24. 24 template = template.substring(0, start) + models.get(key) + template.substring(end + 1);
  25. 25 }
  26. 26
  27. 27 } else {
  28. 28 break;
  29. 29 }
  30. 30
  31. 31 }
  32. 32
  33. 33 return template;
  34. 34
  35. 35 }

 

2) 邮件发送Test-Case

  1. // 1. Resolve html template to real text
  2. String htmlTemplate = "<html lang='zh-cn'><head></head><body><h1>发送带模板数据的Email</h1><p>你好,${username}。本次您的验证码为${code},请妥善保管</p></body></html>";
  3. Map<String, String> models = new HashMap<String, String>();
  4. models.put("username", "XXX");
  5. models.put("code", "4551");
  6. String text = StringUtils.replacePlaceHolder(htmlTemplate, models);
  7. // 2. send email
  8. boolean result = mailHandler.sendText("收件人邮箱", "发送带模板数据的Email", text);
  9. Assert.assertEquals(true, result);

 

 

二、使用Velocity模板

  实现方法:借用VelocityEngineUtils合并Velocity模板和数据,得到要发送的Email, 进行发送。

1) 引入依赖jar包

  1. <!-- velocity -->
  2. <dependency>
  3. <groupId>org.apache.velocity</groupId>
  4. <artifactId>velocity</artifactId>
  5. <version>1.7</version>
  6. </dependency>

 

2) 配置Velocity模板引擎

  1. <!-- Define velocity engine -->
  2. <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  3. <property name="configLocation" value="classpath:velocity.properties" />
  4. </bean>

 

3) 发送邮件实现函数

  1. // Get email content by velocity merge models
    String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, encoding, models);


  2. // Send email
  3. MimeMessage mimeMessage = mailSender.createMimeMessage();
  4. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
  5. helper.setFrom(form); // set sender
  6. helper.setTo(to); // set recipients
  7. helper.setSubject(subject);
  8. helper.setText(content, true); // Indicate the text included is HTML
  9. mailSender.send(mimeMessage);

 

 

三、使用FreeMarker模板

  实现方法:借用FreeMarkerTemplateUtils合并FreeMarker模板和数据,得到要发送的Email, 进行发送。

1) 引入依赖jar包

  1. <dependency>
  2.   <groupId>org.freemarker</groupId>
  3.   <artifactId>freemarker</artifactId>
  4.   <version>2.3.23</version>
  5. </dependency>

 

2)配置FreeMarker

  1. <!-- Define freemarker configuration -->
  2. <bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
  3.   <property name="templateLoaderPath" value="classpath:template/freemarker" />
  4.   <property name="defaultEncoding" value="utf-8" />
  5.   <property name="freemarkerSettings">
  6.   <props>
  7.   <prop key="template_update_delay">10</prop>
  8.   <prop key="locale">zh_CN</prop>
  9.       <prop key="number_format">#.##</prop>
  10.     </props>
  11.   </property>
  12. </bean>

 

3) 发送邮件实现函数

  1. // Get email content by freeMarker template
    Template realTemplate = freeMarkerConfigurer.getTemplate(template);
  2. String content = FreeMarkerTemplateUtils.processTemplateIntoString(realTemplate, models);

  1. // Send email
  2. MimeMessage mimeMessage = mailSender.createMimeMessage();
  3. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
  4. helper.setFrom(form); // set sender
  5. helper.setTo(to); // set recipients
  6. helper.setSubject(subject);
  7. helper.setText(content, true); // Indicate the text included is HTML
  8. mailSender.send(mimeMessage);

 

版权声明:本文为ark-blog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ark-blog/p/8729490.html