如何将多个内联base64编码图像附加到邮件?

时间:2018-08-20 14:34:20

标签: java javamail mime-types multipart mime-message

我有以下代码

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");

但是,当我收到邮件时,邮件中的第一张图像不是显示而是第二张图像。对于第一个图像,它显示为十字形。另外我还需要在邮件中附加多个图像(以base64编码的值)

0 个答案:

没有答案