如何从文件中读取 HTML 模板以使用 JavaMail API 发送邮件?

Ale*_*jic 4 java email jakarta-mail html-email

我有一个使用 JavaMail API 发送 HTML 邮件的任务。这是我的代码的一小部分:

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);

try {
    helper.setTo(recipients);
    helper.setSubject("Simple mail template");
    helper.setText("<html><body>Hi There</body><html>",html:true);
} catch (MessagingException e) {
        e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个任务来将 HTML 移动到一个单独的文件中,并创建一个类来读取该 HTML 模板并用它发送邮件。关于如何做到这一点的任何建议?

Imp*_*Fox 5

使用模板引擎

我用Thymeleaf作为模板引擎创建了一个最小的例子。您首先写道您在项目中使用了Spring Boot,所以我假设您可以使用它。我还假设您使用MavenGradle作为构建工具。


添加 Thymeleaf 依赖项

spring-boot-starter-thymeleaf依赖项添加到您的项目中。你在使用 Maven 还是 Gradle?

马文

您的pom.xml依赖项应包括:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

摇篮

您的build.gradle依赖项应包括:

compile('org.springframework.boot:spring-boot-starter-thymeleaf')
Run Code Online (Sandbox Code Playgroud)

在 Spring Boot 中配置 Thymeleaf

添加所需的@Beans。那些是:

@Bean
public ITemplateResolver templateResolver()
{
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);

    return templateResolver;
}

@Bean
public TemplateEngine templateEngine()
{
    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(this.templateResolver());

    return templateEngine;
}
Run Code Online (Sandbox Code Playgroud)

他们可以进入任何用@Configuration(或@SpringBootApplication)注释的类。


例子

现在,您可以TemplateEngine从其字段由 Spring 注入的任何类访问您的。

@Component
public class SomeClass
{
    @Autowired
    private TemplateEngine templateEngine;

    public String generateMailHtml(String text)
    {
        Map<String, Object> variables = new HashMap<>();
        variables.put("mailtext", text);

        final String templateFileName = "mail"; //Name of the template file without extension
        String output = this.templateEngine.process(templateFileName, new Context(Locale.getDefault(), variables));

        return output;
    }
}
Run Code Online (Sandbox Code Playgroud)

mail.html应设在classpath( resources/)下templates/

模板文件路径

它应该是这样的:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

您发布的代码片段现在可能如下所示(@Autowired SomeClasssomeClass在方法外部的类中):

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);

try {
    helper.setTo(recipients);
    helper.setSubject("Simple mail template");
    helper.setText(someClass.generateMailHtml("Hi There"), true);
} catch (MessagingException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

当然,根据您的需要更改示例!


编辑

你提到你需要“用一些名字的列表”填充模板。这将是这样实现的:

public String generateMailHtml(List<String> names)
{
    Map<String, Object> variables = new HashMap<>();
    variables.put("names", names);

    final String templateFileName = "mail"; //Name of the template file without extension
    String output = this.templateEngine.process(templateFileName, new Context(Locale.getDefault(), variables));

    return output;
}
Run Code Online (Sandbox Code Playgroud)

mail.html

compile('org.springframework.boot:spring-boot-starter-thymeleaf')
Run Code Online (Sandbox Code Playgroud)

阅读更多关于data-th-each/th:each 在这里。注意:您可以使用data-th-th:互换,但data-th-更适合 HTML5。