Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用Spring4和Thymeleaf发送邮件_Html_Spring_Thymeleaf - Fatal编程技术网

Html 使用Spring4和Thymeleaf发送邮件

Html 使用Spring4和Thymeleaf发送邮件,html,spring,thymeleaf,Html,Spring,Thymeleaf,我正在尝试使用Spring4和Thymeleaf模板发送一封简单的HTML邮件。在我的java类中,我有: public void sendMail() throws Exception { if (!flag){ throw new IOException("Mail Sending Forbiden"); } // Prepare the evaluation context final Context ctx = new Context()

我正在尝试使用Spring4和Thymeleaf模板发送一封简单的HTML邮件。在我的java类中,我有:

public void sendMail() throws Exception {
    if (!flag){
        throw new IOException("Mail Sending Forbiden");
    }

    // Prepare the evaluation context
    final Context ctx = new Context();
    List<FacebookFeed> list = getFeedsList();
    ctx.setVariable("message", "message");
    //Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
    message.setSubject("Example HTML email (simple)");
    message.setTo(recipient);

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.htmlTemplateEngine.process("/static/test", ctx);
    System.out.println(htmlContent);
    message.setText(htmlContent, true /* isHtml */);

    // Send email
    this.mailSender.send(mimeMessage);
}
public void sendMail()引发异常{
如果(!标志){
抛出新IOException(“邮件发送禁止”);
}
//准备评估上下文
最终上下文ctx=新上下文();
List List=getFeedsList();
设置变量(“消息”、“消息”);
//使用Spring助手准备消息
final-mimessage-mimessage=this.mailssender.createmimessage();
最终mimessagehelper消息=新的mimessagehelper(mimessage,“UTF-8”);
message.setSubject(“示例HTML电子邮件(简单)”;
message.setTo(收件人);
//使用Thymeleaf创建HTML正文
最终字符串htmlContent=this.htmlTemplateEngine.process(“/static/test”,ctx);
System.out.println(htmlContent);
message.setText(htmlContent,true/*isHtml*/);
//发送电子邮件
this.mailssender.send(mimessage);
}
我在html页面中使用了thymeleaf th:text:

<span th:text="${message}"></span>

但它不起作用。我在模板中得到的都是相同的.html页面。
th:text=“${message}”
-不起作用

有什么想法吗?

我的错误是:

        templateResolver.setTemplateMode(TemplateMode.TEXT);
而不是

        templateResolver.setTemplateMode(TemplateMode.HTML);

如何创建
htmlTemplateEngine
?属性
spring.thymeleaf.suffix
是否设置为
html
?无论如何,如果您可以切换到spring Boot,我建议使用此库:
htmlTemplateEngine
应该是
org.thymeleaf.spring4.SpringTemplateEngine
的一个实例,您应该能够在类中自动连接。如果这是正确的,我将跳转到
过程
方法并进行调试,直到我不明白为什么不使用上下文。请告诉我们这个
模板解决方案
来自哪里?问题中根本没有提到这一点