我正在使用 spring boot、thymleaf、spring MVC、gradlebuild 工具。我想用 thymleaf 模板附件实现 spring 邮件。当我在这个方法中调用 web 服务(restful)来发送电子邮件和这个方法时,我正在调用一个 spring 模板引擎来生成一个 thymleaf 模板来发送电子邮件附件。
为了进一步说明,我附上了一个流程结构。
休息控制器
@RequestMapping(value= "/completeBatchUpload")
public Map<String,String>completeBatchShipmentUpload() throws Exception {
Map<String, String> resp = new HashMap<>();
String s = emailService.sendmail();
//----some code here based on result of emailService.sendmail method.----
return resp;
}
电子邮件服务.java
public String sendmail(){
try {
MimeMessage message = javaMail.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(new InternetAddress("[email protected]"));
helper.setTo(new InternetAddress("[email protected]"));
helper.setSubject("subject here"); helper.setText(attachmentUtile.buildContentUsingTemplate();
} catch (Exception e) {
throw new MailParseException(e);
}
javaMail.send(message);
}
}
@Service
public class AttachmentUtile {
private SpringTemplateEngine templateEngine;
@Autowired
public AttachmentUtile (SpringTemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}
public String buildContentUsingTemplate(List<Map<String, String>> templateContent, String recipientName) {
final Context ctx = new Context();
ctx.setVariable("recipientName", "ritesh");
String s = templateEngine.process("send", ctx);
return s;
}
}
发送.html
使用 Thymeleaf HTML 模板示例发送电子邮件
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<!-- use the font -->
<style>
body {
font-family: 'Roboto', sans-serif;
font-size: 48px;
}
</style>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
<tr>
<td align="center" bgcolor="#78ab46" style="padding: 40px 0 30px 0;">
<p>RITESH CHOUDAHRY</p>
</td>
</tr>
<tr>
<td bgcolor="#eaeaea" style="padding: 40px 30px 40px 30px;">
<p>Dear ${recipientName},</p>
<p>Sending Email using Spring Boot with <b>Thymeleaf template !!!</b></p>
<p>Thanks</p>
</td>
</tr>
</table>
百里香叶配置文件
@Configuration
public class ThymLeafConfig {
@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(htmlTemplateResolver());
return templateEngine;
}
@Bean
public ITemplateResolver htmlTemplateResolver(){
SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
emailTemplateResolver.setPrefix("/templates/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
return emailTemplateResolver;
}
项目文件夹结构
我得到以下异常:
I have seen one link in stackoverflow Error resolving template “login” spring boot, thymeleaf, But still I am not able to solve my problem. If anybosy have have idea please share. Thanks in advance. If any query required please comment.
It seems that you have exactly the same problem as in the link you cite.
From your project structure one can tell that path to template is classpath:/templates/send.thml
as you put your template in resources folder. Later it will land added to your jar/war.
At the same time you configure your prefix to
emailTemplateResolver.setPrefix("/templates/");
在 web 应用程序中将被视为应用程序根目录中的文件夹(不是 web 应用程序类路径所在的 /WEB-INF/classes),在 jar 可启动应用程序中,此路径将被视为相对于基本文件夹(您曾经运行过的文件夹)您的应用程序)。
您应该将该行更新为
emailTemplateResolver.setPrefix("classpath:/templates/");
该classpath:
前缀给人春天ResourceResolver
的提示,你的模板是应用的类路径的资源(应搜索用ClassLoader.getResource()
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句