繁体   English   中英

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

[英]Freemarker is not displaying images on mail templates

我一直在尝试在我的 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. ..

我怎样才能让它在两者上都起作用? 或者至少让它在 gmail 上工作......

模板.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码

@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;
    }

}

如前所述,addInline 根本不起作用,我想让它起作用,所以我不将图像作为附件发送,这甚至不适用于 gmail 无论如何......

您需要更改辅助命令的顺序。 就我而言,我遇到了与您相同的问题。 对于内联附件,您需要使用“helper.addInline”,但顺序很重要。

以下不起作用:

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

但这是有效的:

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

首先,通过“setText”添加 html,之后您可以通过“addInline”添加内联附件。

这对我来说很好

暂无
暂无

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

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