简体   繁体   English

Freemarker 不在邮件模板上显示图像

[英]Freemarker is not displaying images on mail templates

I've been trying to send images in my ftl templates but it is not working.我一直在尝试在我的 ftl 模板中发送图像,但它不起作用。

I tried base64, outlook works - gmail does not work I tried <img src="cid:logo" alt="logo"> outlook does not work - gmail does not work I tried with attachement also using cid outlook works - gmail doesnt... I tried base64, outlook works - gmail does not work I tried <img src="cid:logo" alt="logo"> outlook does not work - gmail does not work I tried with attachement also using cid outlook works - gmail doesnt. ..

How can I maje it work on both?我怎样才能让它在两者上都起作用? Or at least make it work on gmail...或者至少让它在 gmail 上工作......

template.ftl模板.ftl

<!DOCTYPE html>
<html lang="en">
<head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<img src="cid:logo.png" alt="logo">
</body>
</html>

Java code Java码

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private Configuration freemarkerConfig;

    @Autowired
    private JavaMailSender sender;

    @Override
    public MailResponse sendEmail(final MailRequest mail, final String template, final Map<String, Object> model) {
        final MailResponse response = new MailResponse();
        final MimeMessage message = this.sender.createMimeMessage();
        try {
            final MimeMessageHelper helper = new MimeMessageHelper(message, true,
                    StandardCharsets.UTF_8.name());

            final Template t = this.freemarkerConfig.getTemplate(template);

            final String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
            
            // addInline() not working at all...
            // helper.addInline("logo.png", new ClassPathResource("logo.png"));
            helper.addAttachment("logo.png", new ClassPathResource("logo.png"));
            helper.setText(html, true);
            helper.setTo(mail.getTo());
            helper.setSubject(mail.getSubject());
            helper.setFrom(mail.getFrom());
            this.sender.send(message);

            response.setMessage("mail send to : " + mail.getTo());
            response.setSent(Boolean.TRUE);

        } catch (MessagingException | IOException | TemplateException e) {
            response.setMessage("Mail Sending failure : " + e.getMessage());
            response.setSent(Boolean.FALSE);
        }

        return response;
    }

}

As said before, addInline is not working at all, and I would like to make it work so I don't send images as attachments, which is not even working for gmail anyway...如前所述,addInline 根本不起作用,我想让它起作用,所以我不将图像作为附件发送,这甚至不适用于 gmail 无论如何......

You need to change the order of the helper commands.您需要更改辅助命令的顺序。 In my case I had the same problem as you had.就我而言,我遇到了与您相同的问题。 For an inline attachment you need to use "helper.addInline", but the order matters.对于内联附件,您需要使用“helper.addInline”,但顺序很重要。

The following is not working:以下不起作用:

helper.addInline("logo.png", new ClassPathResource("logo.png"));
helper.setText(html, true);

But this is working:但这是有效的:

helper.setText(html, true);
helper.addInline("logo.png", new ClassPathResource("logo.png"));

First, add the html via "setText", after you can add an inline attachment via "addInline".首先,通过“setText”添加 html,之后您可以通过“addInline”添加内联附件。

This worked fine for me这对我来说很好

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

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