当我尝试使用java MIMessage插入一个内联图像时,会出现两个内联图像

vuktfyat  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(151)

我想在电子邮件正文的中间附加一个内联图像。我可以成功地将图像附加到身体上。问题是另一个内联图像出现在电子邮件正文的最末端,这是我不想要的。下面是我的代码。你能帮我找出错误吗?

Message message = new MimeMessage(Session.getInstance(System.getProperties()));
//            message.setFrom(new InternetAddress(from));
          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

          message.setRecipients(Message.RecipientType.CC,InternetAddress.parse("aa@mail.com,bb@mail.com"));

          message.setSubject(subject);
          MimeBodyPart content = new MimeBodyPart();
          content.setText(body); //body is a html string
          content.setHeader("Content-Type", "text/html");
          Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(content);
          // add attachments
          String filename = projectName.replace('/','_')+".png";

          MimeBodyPart messageBodyPart = new MimeBodyPart();
          messageBodyPart.setHeader("Content-ID", "<" +filename+">");
          messageBodyPart.setDisposition(MimeBodyPart.INLINE);
          messageBodyPart.setFileName(projectName.replace('/','_')+".png");

          DataSource source = new FileDataSource(new File(barChart+filename));
          messageBodyPart.setDataHandler(new DataHandler(source));
          multipart.addBodyPart(messageBodyPart);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题