Search code examples
javathymeleafjakarta-mail

Java Mail: Inline Image is not shown in email


I am trying to add inline image like this:
Static folder location: resources-> static

MimeMessagePreparator messagePreparator = mimeMessage -> {
                MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
                messageHelper.setTo(recipientList);
                messageHelper.setFrom(fromEmail);
                messageHelper.setSubject(emailInfo.getSubject());
                //ClassPathResource image = new ClassPathResource("static/pin.png");
                FileSystemResource image = new FileSystemResource(new File("static/pin.png"));
    
    
               messageHelper.addInline("pin_image",image);
    
                messageHelper.setText(body, true);
    
            };
            sendMail(messagePreparator, emailInfo);

Edit: thymeleaf related part:

    <img src="cid:pin_image" alt="location" width="25" height="25" border="0"
style="display: block; font-family: Arial; color: #666666; font-size: 14px; width: 25px; height: 25px;">

Email body:

enter image description here
The last image in the email-body is where I set pin_image. But I get no inline image in my email! I am using using thymeleaf template where I set contentId pin_image as in java code. What am I doing wrong?


Solution

  • I solved just altering the orders of these two lines:

     messageHelper.addInline("pin_image",image);
     messageHelper.setText(body, true);
    

    I think, I was trying to create an contentId when the HTML is not yet provided. That's why was facing the issue.

    P.S: My whole HTML was in the body variable which is my template