0

I have the following code

final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper;
    try { 
      helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
      String htmlMsg = "<body><div style="padding-top: 1px"><table 
       style="width: 100%">  <tr> <td align="left"><img align="left" 
       src="cid:**image1**" style="margin-left: -7px; width: 50%;"></td>                                    
       <td align="right"><img align="right" src="cid:**image2**"                    
      style="margin-left: 100px; width: 50%;"> </td>   </tr> </table></div> 
       <body>"



        Multipart multipart = new MimeMultipart("related");
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(htmlMsg, "text/html");
        messageBodyPart.setDisposition(BodyPart.INLINE);
        multipart.addBodyPart(messageBodyPart);
        InternetHeaders headers = new InternetHeaders();
        headers.addHeader("Content-Type", "image/png");
        headers.addHeader("Content-Transfer-Encoding", "base64");
        String base64EncodedImageContentByteArray="encdoe value";
        String base64EncodedImageContentByteArray1 = "encoded value";
        MimeBodyPart imagePart1 = new MimeBodyPart(headers, 
        base64EncodedImageContentByteArray.getBytes());
        imagePart1.setDisposition(MimeBodyPart.INLINE);
        imagePart1.setContentID("image1");
        imagePart1.setFileName("image1.jpg");
        MimeBodyPart imagePart2 = new MimeBodyPart(headers, 
        base64EncodedImageContentByteArray1.getBytes());
        imagePart2.setDisposition(MimeBodyPart.INLINE);
        imagePart2.setContentID("image2");
        imagePart2.setFileName("image2.jpg");
        multipart.addBodyPart(imagePart1);
        multipart.addBodyPart(imagePart2);
        multipart.addBodyPart(messageBodyPart);
        mimeMessage.setContent(multipart);
        mimeMessage.setHeader("MIME-Version", "1.0");

But when I received the mail the first image is not showing but the second image showing in the mail. For first image place it is showing cross. Also I need to attache multiple images(base64 encoded value) in the mail

Subhajit Pal
  • 565
  • 1
  • 8
  • 19
  • You should not encode in base64 yourself, you should leave that to JavaMail, and otherwise, look at this answer for inspiration: https://stackoverflow.com/a/28992525/466862 – Mark Rotteveel Aug 20 '18 at 15:57
  • OK. But how to send multiple images, as my image is stored in DB as CLOB. – Subhajit Pal Aug 20 '18 at 15:59
  • 1
    I used a comment, as it was a suggestion not directly related to your problem (although it could be). I would need to test this to find out what a solution would be as I don't normally work with inline images, and I don't have the time for that. I do see a lot of odd things in your code though (using the same `InternetHeaders` instance for both attachments, adding `messageBodyPart` twice, setting disposition inline for the message body, explicitly setting the mime version, your odd explicitly forcing base64, etc). They could all contribute to your problem (or maybe not ...). – Mark Rotteveel Aug 20 '18 at 16:10
  • Yes, there's a lot of extraneous operations in your code. This [JavaMail FAQ entry](https://javaee.github.io/javamail/FAQ#sendmpr) should help you simplify things. – Bill Shannon Aug 20 '18 at 19:02
  • By using the multiple InternetHeaders my problem resolved. – Subhajit Pal Aug 21 '18 at 11:01

0 Answers0