import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import java.util.HashMap;
import java.util.Map;
public class EmailService {
public static void main(String[] args) {
// Define the template string
String emailTemplateString = "Hello ${greetings},\n\nHere is your report: ${subject}.\n\nBest regards,\n${team}\n\n(Email Subject: ${name})";
// Create the context and set variables
Context context = new Context();
Map<String, Object> variables = new HashMap<>();
variables.put("greetings", "Dear Frodo");
variables.put("subject", "Sub Custody Report");
variables.put("team", "Ring of the fellowship");
variables.put("name", "Aragorn");
context.setVariables(variables);
// Set up the template engine
TemplateEngine textTemplateEngine = new TemplateEngine();
StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);
stringTemplateResolver.setCacheable(false);
textTemplateEngine.setTemplateResolver(stringTemplateResolver);
// Process the template
String processedTemplate = textTemplateEngine.process(emailTemplateString, context);
// Print the result
System.out.println(processedTemplate);
}
}
Getting response like this:
"Hello ${greetings},\n\nHere is your report: ${subject}.\n\nBest regards,\n${team}\n\n(Email Subject: ${name})"
I know that template is not a .txt file but as far as I read it should be possible to make it from String and for such case need to use TemplateMode.TEXT although non of them placing values.
You need to sign in to view this answers