1

I have to send email from Java application. The email content is a HTML file with images. When I load in my application, I am able to see the images. But after sending it as an email, I am not able to see the images inside the HTML in email. I have tried attaching those images as an inline images. It is not working. I can see those images in attachment but not inside the content/body. Please help me. Please see the code below.

        Map<String, String> inlineImages = new HashMap<String, String>();
        for (int co=0; co<attachments.size(); co++){
             inlineImages.put(Math.random()+"", "E:\\eclipse-workspace\\Demo\\src\\images\\EmailTemplatesOutput\\assets\\"+attachments.get(co));
        }

        // adds inline image attachments
        if (inlineImages != null && inlineImages.size() > 0) {
            Set<String> setImageID = inlineImages.keySet();

            for (String contentId : setImageID) {
                MimeBodyPart imagePart = new MimeBodyPart();
                imagePart.setHeader("Content-ID", "<" + contentId + ">");
                imagePart.setDisposition(MimeBodyPart.INLINE);

                String imageFilePath = inlineImages.get(contentId);
                try {
                    imagePart.attachFile(imageFilePath);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                multipart.addBodyPart(imagePart);
            }
        }
            emailMessage.setContent(multipart);

enter image description here

Kutty
  • 712
  • 1
  • 5
  • 22
  • How about [this](https://stackoverflow.com/questions/2996514/inline-images-in-email-using-javamail), seems extremely helpful with external links – buræquete Nov 03 '18 at 11:01
  • See this: https://www.tutorialspoint.com/javamail_api/javamail_api_send_inlineimage_in_email.htm –  Nov 03 '18 at 11:02
  • 1
    It's possible that your email settings on gmail are automatically blocking the images. You should try https://mailtrap.io/ for testing emails. Most issues can be found through their tools. – CecilMerrell aka bringrainfire Nov 03 '18 at 11:05
  • You're generating the Content-Ids as random numbers, how do you expect the html to reference those images using the Content-Id? All the references to the images in the html need to use a URL of the form "cid:{content-id}" instead of (e.g.) a file: URL. – Bill Shannon Nov 03 '18 at 21:55

0 Answers0