title: How to send email with inline image using ThymeLeaf
body: <p>I'm trying to send an email with an inline image using ThymeLeaf and Spring, but so far no success. The email sends, but the inline image won't show in the email.</p>
  
  <p>The project is not web-based (not a website), but is a desktop stand-alone, not mobile</p>
  
  <p>This is how I get the image file:</p>
  
  <pre><code>URL url = getClass().getResource("/LawFirmAdvisoryGroup.jpg");
  File file = new File(url.getPath());
  
  MultipartFile multipartFile = new MockMultipartFile(file.getName(),
      file.getName(), "image/jpeg", IOUtils.toByteArray(input));
  </code></pre>
  
  <hr>
  
  <p>My service class:</p>
  
  <pre><code>@Autowired
  private JavaMailSender mailSender;
  
  @Autowired
  private TemplateEngine templateEngine;
  
  public void sendMailWithInline(final String recipientName, final String recipientEmail, final MultipartFile image, final byte[] imageBytes)
  throws MessagingException {
  
      final Context ctx = new Context();
          ctx.setVariable("imageResourceName", image.getName()); // so that we can reference it from HTML
  
          final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
          final MimeMessageHelper message
          = new MimeMessageHelper(mimeMessage, true, "UTF-8");
          message.setSubject("Inline Image");
          message.setFrom("XXXX@yahoo.com");
          message.setTo(recipientEmail);
  
          // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
          final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
          message.addInline(image.getName(), imageSource, image.getContentType());
  
  
          final String htmlContent = this.templateEngine.process("left_sidebar.html", ctx);
          message.setText(htmlContent, true);
          this.mailSender.send(mimeMessage);
  
      }
  </code></pre>
  
  <hr>
  
  <p>The HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
    &lt;head&gt;
      &lt;title th:remove="all"&gt;Email with inline image&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;
        &lt;img src="LawFirmAdvisoryGroup.jpg" th:src="'cid:' + ${imageResourceName}" /&gt;
      &lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>