how to send html content at place of thymeleaf template name

Viewed 1482

I want to send html content in email as text and want to bind some variables into this html content. this html content is saved in my database which i used to send email. so i want to use SpringTemplateEngine's process method which accept template name, and context variables. but i have html content not a template. i want to use thymeleaf template engine for this. but i doesn't understand how can i convert html content for example :

<html>
<head></head>
<title></title>
<body>
<div>
<p>Your ${value1} for ${value2}</p>
</div
</body>

to thymeleaf template by using following method:

String mailTemplate = templateEngine.process("mailTemplate", context);

Please someone help me to overcome this problem.

1 Answers

There is an StringTemplateResolver which we can use to send html content as string at place of template name.

For example:

Configure your template resolver and template engine

private ITemplateResolver stringTemplateResolver() {
    final StringTemplateResolver templateResolver = new StringTemplateResolver();
    templateResolver.setTemplateMode(TemplateMode.HTML);
    return templateResolver;
}

@Bean
public TemplateEngine emailTemplateEngine() {
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(stringTemplateResolver());
    return templateEngine;
}

and use this in your email service. this solved your problem.

Related