Tomáš Červenka
Tomáš Červenka

Reputation: 35

Email inline image in email not working on MacOS email

I'm sending HTML emails from java, using spring boot. Emails include signature with image logo of our firm. It works perfectly fine f.e. on gmail. But in MacOS app Email the logo is sent as attachment and is not inlined.

Non-related parts of code replaced by ...

final Locale locale = ...;
final MimeMessage mail = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
    helper = new MimeMessageHelper(mail, true, "UTF-8");
    helper.setTo(...);
    helper.setCc(...);
    helper.setBcc(...);
    helper.setFrom(...);

    final String htmlContent = templateEngine.process(..., new Context(locale, ...));
    helper.setText(htmlContent, true);
    helper.addInline("myImg", new ClassPathResource(".../myImg.png"));
} catch (UnsupportedEncodingException | MessagingException e) {
    throw new MailSendException(..., e);
}
javaMailSender.send(mail);

HTML is generated by thymeleaf and is following:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
...
<img src=".../myImg.png" th:src="'cid:' + ${'myImg'}" />

</body>
</html>

Upvotes: 0

Views: 540

Answers (1)

JeanValjean
JeanValjean

Reputation: 17713

Have a look at the code of this extension Spring Boot Email Tools, where maybe you can find a better way to create emails.

Personally, I think that you may save some time if you use it directly to send emails via the EmailService bean. From the docs (the example is based on Freemarker template engine):

@Autowired
public EmailService emailService;

public void sendEmailWithTemplating(){
   Arrays.asList(new Cospirator("[email protected]", "Gaius Cassius Longinus"),
            new Cospirator("[email protected]", "Marcus Iunius Brutus Caepio"))
        .stream.forEach(tyrannicida -> {
       final Email email = DefaultEmail.builder()
            .from(new InternetAddress("[email protected]", "Gaius Iulius Caesar"))
            .to(Lists.newArrayList(new InternetAddress(tyrannicida.getEmail(), tyrannicida.getName())))
            .subject("Idus Martii")
            .body("")//Empty body
            .encoding(Charset.forName("UTF-8")).build();
        //Defining the model object for the given Freemarker template
        final Map<String, Object> modelObject = new HashMap<>();
        modelObject.put("tyrannicida", tyrannicida.getName());

       emailService.send(email, "idus_martii.ftl", modelObject);
   };
}

private static class Cospirator {
  private String email;
  private String name;
  public Cospirator(final String email, final String name){
    this.email = email;
    this.name = name;
  }

  //getters
}

The dependencies you need are

<dependency>
    <groupId>it.ozimov</groupId>
    <artifactId>spring-boot-email-core</artifactId>
    <version>0.4.0</version>
</dependency>
<dependency>
    <groupId>it.ozimov</groupId>
    <artifactId>spring-boot-thymeleaf-email</artifactId>
    <version>0.4.0</version>
</dependency>

Pretty convenient, ain't it?

Upvotes: 1

Related Questions