Javamail多部分未正确排序

时间:2015-04-14 03:51:19

标签: image javamail inline mime

我有一个应用程序尝试动态构建Javamail消息,组装在邮件时可用的Mime正文部分。每个图像的顶部都有一个'GPC'图像,后面是一些HTML文本(正文),一个由HTML构建的结束,一个结束的'Brand'图像,以及结束的最后一部分,也是HTML。文件附件可能包括也可能不包括在内。如果适用,错误免责声明(HTML)可能位于第一个图像之前。

免责声明,正文,结束,&附件是主体部分,而图像是嵌套的多部分,由包含CID的HTML容器和图像主体部分本身组成。发送电子邮件后,它在Gmail& Lotus Notes,在Outlook(Hotmail)中,GPC图像被顶部的Brand图像覆盖。身体部位的顺序保留,但图像除外,它们似乎总是回复到顶部。

我无法弄清楚这一点。在我看来,'父'多部分应该是子类型'混合',因为身体部位的顺序很重要,并且它们是相对无关的,而嵌套的多部分是'相关的',因为一个身体部分引用另一个。我的代码:

public MimeMultipart email_content_builder() throws MessagingException {
   MimeMultipart mp = new MimeMultipart();
   MimeBodyPart txt_part = new MimeBodyPart(), att_part, err_part, gpc_img_location_part, gpc_img_part, brand_img_location_part, brand_img_part, closing_pt1_part, closing_pt2_part;
   DataSource att_source, gpc_img_src, brand_img_src;
   String gpc_img_container_html, brand_img_container_html;

  //Insert error disclaimer, if applicable:
   if (!error_disclaimer.isEmpty()) {
      err_part = new MimeBodyPart();
      err_part.setText(error_disclaimer, "ISO-8859-1", "html");
      mp.addBodyPart(err_part);
   }

  //Insert GPC logo image, if applicable:
   if (GPC_logo.length > 0) {
      MimeMultipart gpc_imagery_mp = new MimeMultipart("related");
      MimeBodyPart gpc_imagery_part = new MimeBodyPart();

     //When resizing the GPC image, the height should be roughly 23% of the width;  use this factor:  .2331
      gpc_img_container_html = "<html><body><img src='cid:gpc_logo' height=\"23px\" width=\"100px\"></body></html>";

      gpc_img_location_part = new MimeBodyPart();
      gpc_img_location_part.setContent(gpc_img_container_html, "text/html");

      gpc_imagery_mp.addBodyPart(gpc_img_location_part);

      gpc_img_part = new MimeBodyPart();
      gpc_img_src = new ByteArrayDataSource(GPC_logo, "image/jpeg");
      gpc_img_part.setDataHandler(new DataHandler(gpc_img_src));
      gpc_img_part.setHeader("Content-ID", "<gpc_logo>");
      gpc_img_part.setDisposition(MimeBodyPart.INLINE);

      gpc_imagery_mp.addBodyPart(gpc_img_part);
      gpc_imagery_part.setContent(gpc_imagery_mp);
      mp.addBodyPart(gpc_imagery_part);
   }

  //Insert main body of email:
   txt_part.setText(body, "ISO-8859-1", "html");
   mp.addBodyPart(txt_part);

  //Insert the first part of the closing, if applicable:
   if (!Closing_Part1.isEmpty()) {
      closing_pt1_part = new MimeBodyPart();
      closing_pt1_part.setText(Closing_Part1, "ISO-8859-1", "html");
      mp.addBodyPart(closing_pt1_part);
   }

  //Insert brand logo image, if applicable:
   if (brand_logo.length > 0) {
      MimeMultipart brand_imagery_mp = new MimeMultipart("related");
      MimeBodyPart brand_imagery_part = new MimeBodyPart();

     //When resizing the brand image, the height should be roughly 43% of the width;  use this factor:  .4294
      brand_img_container_html = "<html><body><img src='cid:brand_logo' height=\"64px\" width=\"150px\"></body></html>";

      brand_img_location_part = new MimeBodyPart();
      brand_img_location_part.setContent(brand_img_container_html, "text/html");

      brand_imagery_mp.addBodyPart(brand_img_location_part);

      brand_img_part = new MimeBodyPart();
      brand_img_src = new ByteArrayDataSource(brand_logo, "image/jpeg");
      brand_img_part.setDataHandler(new DataHandler(brand_img_src));
      brand_img_part.setHeader("Content-ID", "<brand_logo>");
      brand_img_part.setDisposition(MimeBodyPart.INLINE);

      brand_imagery_mp.addBodyPart(brand_img_part);
      brand_imagery_part.setContent(brand_imagery_mp);
      mp.addBodyPart(brand_imagery_part);
   }

  //Insert the second part of the closing, if applicable:
   if (!Closing_Part2.isEmpty()) {
      closing_pt2_part = new MimeBodyPart();
      closing_pt2_part.setText(Closing_Part2, "ISO-8859-1", "html");
      mp.addBodyPart(closing_pt2_part);
   }

  //Insert attachments, if applicable:
   if (attachments != null) {
      for (int j = 0; j < attachments.size(); j++) {
         att_part = new MimeBodyPart();

         att_source = new FileDataSource((attachments.get(j)).getPath());
         att_part.setDataHandler(new DataHandler(att_source));
         att_part.setFileName((attachments.get(j)).getPath());

         mp.addBodyPart(att_part);
      }
   }
   return mp;
}

1 个答案:

答案 0 :(得分:1)

您对邮件阅读器在邮件中显示多个正文部分的方式控制非常有限。最好的办法是将所有非附件内容放入单个text / html正文部分,使用html格式化消息。您可以使用multipart / related在邮件中包含图像,但在网络上引用图像通常更简单。