简体   繁体   English

图片以百里香附在邮件中

[英]Images are attached in mail with thymeleaf

I'm sending a mail with template using thymeleaf, but the images are being attached, it cannot happen. 我正在使用百里香叶发送带有模板的邮件,但是图像被附加了,无法发生。 Here is my code: 这是我的代码:

HTML: HTML:

<!-- I need put the image backgroud via css!-->
<body th:style="'background-image: url('+ |cid:${background}| +')'">
    <!--my image-->
    <img src="../../../static/images/email/topo_email.png" th:src="|cid:${logo}|"/>
</body>

JAVA: JAVA:

//the main code of the method is here:

String emailFormatado = contatoEmail.getDescricao().replace(System.lineSeparator(), "<br>");
contatoEmail.setDescricao(emailFormatado);

Context context = new Context(new Locale("pt", "BR"));
context.setVariable("contatoEmail", contatoEmail);
context.setVariable("logo", "logo");
context.setVariable("background", "background");

try {
    String corpoEmail = thymeleaf.process("admin/mail/EmailResposta", context);
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    helper.setFrom(contatoEmail.getUsuario().getEmail());
    helper.setTo(email);
    helper.setSubject(String.format("Mensagem de respota"));
    helper.setText(corpoEmail, true);

    helper.addInline("background", new ClassPathResource("static/images/email/background_email.png"));
    helper.addInline("logo", new ClassPathResource("static/images/email/topo_email.png"));

    mailSender.send(mimeMessage);
} catch (MessagingException e) {
    logger.error("Erro enviando e-mail", e);
}

Everything is working corretly, but the images are attached. 一切都正常进行,但图像已附加。 I hope you can help me. 我希望你能帮助我。

The solution: 解决方案:

In my java code, where I put the variable of the image, was missing the type of image: 在我的Java代码中,我将图像的变量放在其中,但是缺少图像的类型:

helper.addInline("logo", new ClassPathResource("static/images/email/topo_email.png"), "image/png");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM