Image Javamail多部分排序不正确
我有一个应用程序,它尝试动态构建一个Javamail消息,组装邮件时可用的Mime主体部分。每个图像的顶部都有一个“GPC”图像,后面是一些HTML文本(正文),一个由HTML构建的结束语,一个结束语“品牌”图像,以及结束语的最后一部分,也是HTML。文件附件可能包括,也可能不包括。如果适用,错误免责声明(HTML)可能位于第一张图像之前 免责声明、正文、结束和附件是正文部分,而图像是嵌套的多部分,由包含CID的HTML容器和图像正文部分本身组成。发送电子邮件时,它在Gmail和Lotus Notes中显示正确,而在Outlook(Hotmail)中,GPC图像在顶部被品牌图像覆盖。身体各部分的顺序被保留下来,除了图像,它们似乎总是恢复到顶部 我想不出来。在我看来,“父”多部分应该是“混合”子类型,因为身体部位的顺序很重要,它们相对不相关,而嵌套多部分是“相关”的,因为一个身体部位引用另一个身体部位。我的代码:Image Javamail多部分排序不正确,image,jakarta-mail,inline,mime,Image,Jakarta Mail,Inline,Mime,我有一个应用程序,它尝试动态构建一个Javamail消息,组装邮件时可用的Mime主体部分。每个图像的顶部都有一个“GPC”图像,后面是一些HTML文本(正文),一个由HTML构建的结束语,一个结束语“品牌”图像,以及结束语的最后一部分,也是HTML。文件附件可能包括,也可能不包括。如果适用,错误免责声明(HTML)可能位于第一张图像之前 免责声明、正文、结束和附件是正文部分,而图像是嵌套的多部分,由包含CID的HTML容器和图像正文部分本身组成。发送电子邮件时,它在Gmail和Lotus No
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;
}
public mimemultippart email\u content\u builder()抛出消息异常{
MimeMultipart mp=新的MimeMultipart();
MimeBodyPart txt_part=新的MimeBodyPart()、附件部分、错误部分、gpc_img_位置部分、gpc_img_部分、品牌_img_位置部分、品牌_img_部分、关闭_pt1_部分、关闭_pt2_部分;
数据源att_source、gpc_img_src、brand_img_src;
字符串gpc\u img\u container\u html、brand\u img\u container\u html;
//插入错误免责声明(如适用):
如果(!error\u disclaimer.isEmpty()){
err_part=新的MimeBodyPart();
err_part.setText(错误免责声明,“ISO-8859-1”,“html”);
mp.addBodyPart(错误部分);
}
//插入GPC徽标图像(如适用):
如果(GPC_logo.length>0){
MimeMultipart gpc_Images_mp=新的MimeMultipart(“相关”);
MimeBodyPart gpc_images_part=新MimeBodyPart();
//调整GPC图像大小时,高度应大约为宽度的23%;使用此系数:.2331
gpc_img_container_html=“”;
gpc_img_location_part=新的MimeBodyPart();
gpc_img_location_part.setContent(gpc_img_container_html,“text/html”);
gpc\u图像\u mp.addBodyPart(gpc\u图像\u位置\u part);
gpc_img_part=新的MimeBodyPart();
gpc_img_src=新的ByteArrayDataSource(gpc_徽标,“图像/jpeg”);
setDataHandler(新的DataHandler(gpc_img_src));
gpc_img_part.setHeader(“内容ID”,下称“);
gpc_img_part.setDisposition(MimeBodyPart.INLINE);
gpc\U图像\u mp.addBodyPart(gpc\u图像\u part);
gpc_Images_part.setContent(gpc_Images_mp);
mp.addBodyPart(gpc_图像_部分);
}
//插入电子邮件正文:
txt_part.setText(正文,“ISO-8859-1”、“html”);
mp.addBodyPart(txt\U部分);
//插入结束语的第一部分(如适用):
如果(!Closing_Part1.isEmpty()){
关闭_pt1_part=新的MimeBodyPart();
结束部分SETEXT(结束部分1,“ISO-8859-1”,“html”);
mp.addBodyPart(结束部分);
}
//插入品牌徽标图像(如适用):
如果(品牌标志长度>0){
MimemMultipart品牌图片=新的MimemMultipart(“相关”);
MimeBodyPart品牌\图像\零件=新的MimeBodyPart();
//调整品牌形象大小时,高度应约为宽度的43%;使用此系数:.4294
品牌_img_container_html=“”;
品牌、位置、零件=新的MimeBodyPart();
brand_img_location_part.setContent(brand_img_container_html,“text/html”);
品牌图片添加车身部件(品牌图片位置部件);
brand_img_part=新的MimeBodyPart();
brand_img_src=新的ByteArrayDataSource(brand_徽标,“图像/jpeg”);
brand_img_part.setDataHandler(新的DataHandler(brand_img_src));
brand_img_part.setHeader(“Content ID”,”);
brand_img_part.setDisposition(MimeBodyPart.INLINE);
品牌形象添加车身部件(品牌形象部件);
品牌形象部分内容(品牌形象部分);
mp.添加车身零件(品牌零件);
}
//插入结束语的第二部分(如适用):
如果(!Closing_Part2.isEmpty()){
关闭_pt2_part=新的MimeBodyPart();
关闭第2部分(关闭第2部分,“ISO-8859-1”,“html”);
mp.添加车身部件(关闭pt2部件);
}
//插入附件(如适用):
如果(附件!=null){
对于(int j=0;j
对于不同的邮件阅读器如何显示邮件中的多个正文部分,您的控制非常有限。最好是将所有非附件内容放在一个文本/html正文部分中,使用html格式化消息。您可以使用multipart/related在邮件中包含图像,但在web上引用图像通常更简单。谢谢比尔。我现在正朝着这个方向努力,尽管在本练习开始时,我不知道是否可以在某个web服务器上托管这些图像。我现在正在调查这种可能性。参考网络上的图片解决了这个问题。谢谢