JavaMailSender-将动态值设置为邮件模板
我在数据库JavaMailSender-将动态值设置为邮件模板,java,spring,email,Java,Spring,Email,我在数据库MailTemplate中有一个实体,它有一个用于某些操作的邮件模板。现在,我需要在这个模板中设置一个值。模板是一个简单的JSP页面,其中包含几个我希望在运行时填充的字段。 例如,模板是: “你好,用户${username}!”现在我想在向用户发送此邮件之前设置用户名值。有可能这样做吗?我应该使用哪些工具?典型的解决方案是使用Spring中广泛支持的模板库之一创建电子邮件内容。下面我列出了使用我最喜欢的库动态填充电子邮件内容的基本步骤: 确保Velocity库JAR位于类路径内 创建
MailTemplate
中有一个实体,它有一个用于某些操作的邮件模板。现在,我需要在这个模板中设置一个值。模板是一个简单的JSP页面,其中包含几个我希望在运行时填充的字段。
例如,模板是:
“你好,用户${username}!”现在我想在向用户发送此邮件之前设置用户名值。有可能这样做吗?我应该使用哪些工具?典型的解决方案是使用Spring中广泛支持的模板库之一创建电子邮件内容。下面我列出了使用我最喜欢的库动态填充电子邮件内容的基本步骤:
- 确保Velocity库JAR位于类路径内
- 创建您的模板文件,它将是某个包下的常见HTML文件,例如some/package/templates/hello.HTML
- 最后一步是确保在应用程序上下文文件中声明
bean定义(注意,我没有介绍您应该自己编写的缺少的邮件bean,这只是一个品味问题,但是可以使用Java配置而不是XML配置注入相同的bean):org.springframework.ui.velocity.VelocityEngineFactoryBean
有关更多详细信息,请参阅谢谢您的帮助。我有一个问题-如果我的模板在数据库中,而不是在单独的包中,该怎么办?如何将其与方法mergeTEmplateIntoString()中的“模板位置”相连接?如何使用spring引导和注释而不是xml文件来完成。我更新了答案以匹配动态内容解析案例,但很难解决配置问题。同时,线程应始终关注单个问题,这样您就可以打开另一个问题来报告与电子邮件模板无关的所有问题…Just ping我和我很乐意帮助。再次感谢。我已经为我的Spring boot创建了一个Velocity配置,所以我希望我已经解决了这个问题,但如果没有,我将创建新主题并请求您的帮助。在您更新的示例中,如果我有一个作为JSP页面保存在数据库中的模板,我还能在“邮件正文”中使用它吗?使用DataSourceSourceLoader做什么?我的目标是使用邮件模板,它始终是从数据库获取的JSP页面。我是否正确,我应该使用JSP内容创建字符串,并设置正确的MIME来执行此操作?<html> <body> <div>Hello user ${username}!</div> </body> </html>
@Autowired JavaMailSenderImpl sender; // ... MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo("someone@host1.com"); message.setFrom("me@host2.com"); Map model = new HashMap(); model.put("username", "Foo"); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "some/package/templates/hello.html", model); message.setText(text, true); //... sender.send(message);
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Missing Java Mail Beans --> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </value> </property> </bean> </beans>
import org.apache.velocity.app.Velocity; //... @Autowired JavaMailSenderImpl sender; // ... String mailBody = "Hello user ${username}!"; // This will depend on how you would get your mail content MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo("someone@host1.com"); message.setFrom("me@host2.com"); // Create the context for data input VelocityContext context = new VelocityContext(); context.put("username", "Foo"); // Create the Writer you would use as the output StringWriter writer = new StringWriter(); // Evaluate your text entry Velocity.evaluate(context, writer, "EvalError", mailBody); message.setText(writer.toString(), true); //... sender.send(message);