1

I have a piece of code where I am trying to send emails templated with thymeleaf to show data and image(as logo) inside the email.

Here is my function to send the email:

    public void sendingemailtest() throws MessagingException, IOException{

    String recipientName = "Hero";
    final Context context = new Context();
    final MimeMessage mimeMessage = this.mailSenderTest.createMimeMessage();
    final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); 
    context.setVariable("name", recipientName);
    context.setVariable("image", "logo.jpg");
    helper.setFrom("from email test");
    helper.setSubject("testing sending email with image");
    helper.setTo("to email test");
    final String htmlContent = this.springTemplateEngine.process("registration-email", context);
    helper.setText(htmlContent, true);      
    helper.addInline("logo.jpg", new FileSystemResource("/logo.jpg"), "image/jpg");
    this.mailSenderTest.send(mimeMessage);

}

And in thymeleaf template I use th:src="|cid:${image}|" to reference the image and th:text="${name}" to reference the name

All is working and the email is sent but I have one issue here: the email when received is showing as containing attachment but I dont want to show the image as attachement. How to do that as I see a lot of companies send emails containing images but not showing attachment sign on the side of the email.

Please help!

msf6012
  • 119
  • 2
  • 13
  • may be you can use html formated mail content if I m not mistaking – Mohammed Housseyn Taleb May 07 '18 at 22:17
  • https://stackoverflow.com/questions/5068827/how-do-i-send-an-html-email – Mohammed Housseyn Taleb May 07 '18 at 22:17
  • https://stackoverflow.com/questions/1851728/how-to-embed-images-in-html-email – Mohammed Housseyn Taleb May 07 '18 at 22:18
  • thank you @MohammedHousseynTaleb but your suggestion doesnt work as I am not creating my html template inside my function. I have a template seperately and as you see in my code I use template resolver to process the template and put data and image inside it. I wont use set text and create a html code inside my function. (I have design and too many stuff inside my html email document) – msf6012 May 07 '18 at 22:37

0 Answers0