我正在开发一个Java Spring Boot Web App,用户可以在其中注册。他们注册时会收到一封确认电子邮件。当我尝试获取电子邮件中的链接以实际确认注册时,此方法不再起作用,因为我收到错误消息:“模板解析期间发生错误(模板:“ mail / verifyEmail.html”)”。我不确定为什么会这样。我已经在verifyEmail.html文件下面附加了文件,然后附加了附带的EmailService.java文件,该文件具有EmailService()和sendVerificationEmail()方法:
<!DOCTYPE html>
<html>
<th:block xmlns:th="http://www.thymeleaf.org">
<span th:text="'Hello ' + ${name}"></span>
<p>Thank you for registering for the bcoreHW site!</p>
<p>Please click <a href="#">here</a> to verify your email address.</p>
</th:block>
</html>
import java.util.Date;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.enable}")
private boolean enable;
private TemplateEngine templateEngine;
private void send(MimeMessagePreparator preparator) {
if(enable) {
mailSender.send(preparator);
}
}
@Autowired
public EmailService(TemplateEngine templateEngine) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("mail/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(false);
templateEngine.setTemplateResolver(templateResolver);
this.templateEngine = templateEngine;
}
public void sendVerificationEmail(final String emailAddress) {
Context context = new Context();
context.setVariable("name", "Bob");
final String emailContents = templateEngine.process("verifyEmail", context);
System.out.println(emailContents);
MimeMessagePreparator preparator = new MimeMessagePreparator() {
// not working for some reason: @Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(emailAddress);
message.setFrom(new InternetAddress("no-reply@testSite.com"));
message.setSubject("Please Verify Your Email Address");
message.setSentDate(new Date());
message.setText(emailContents, true);
}
};
send(preparator);
}
}
有人看到这有什么问题吗?谢谢。另外,这是我收到的错误消息:
Caused by: java.io.FileNotFoundException: ClassLoader resource "mail/verifyEmail.html" could not be resolved
at org.thymeleaf.templateresource.ClassLoaderTemplateResource.reader(ClassLoaderTemplateResource.java:130) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
答案 0 :(得分:0)
我发现了问题。我的前缀“ mail /”不正确,因为它实际上在名为“ mailTemplates”的目录中。愚蠢的我。