简体   繁体   中英

Freemarker is not displaying images on mail templates

I've been trying to send images in my ftl templates but it is not working.

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...

template.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

@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...

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.

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".

This worked fine for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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