Java 邮件中嵌入的图像 - CID 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10191109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image embedding in Java mail - CID not working
提问by krobbens
I'm working on an application to send mails to lists etc. It's written in Java using the javax.mail api. The problem here is, I can't embed images using a CID.
我正在开发一个将邮件发送到列表等的应用程序。它是使用 javax.mail api 用 Java 编写的。这里的问题是,我无法使用 CID 嵌入图像。
This is the source of the mail that is sent:
这是发送的邮件的来源:
Delivered-To: [email protected]
Received: by 10.43.50.4 with SMTP id vc4csp85536icb;
Tue, 17 Apr 2012 05:08:00 -0700 (PDT)
Received: by 10.204.141.25 with SMTP id k25mr4406030bku.72.1334664479298;
Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Return-Path: <[email protected]>
Received: from mail-bk0-f43.google.com (mail-bk0-f43.google.com [209.85.214.43])
by mx.google.com with ESMTPS id ad16si7984294bkc.150.2012.04.17.05.07.58
(version=TLSv1/SSLv3 cipher=OTHER);
Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Received-SPF: pass (google.com: domain of [email protected] designates 209.85.214.43 as permitted sender) client-ip=209.85.214.43;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of - designates 209.85.214.43 as permitted sender) smtp.mail=-; dkim=pass [email protected]
Received: by mail-bk0-f43.google.com with SMTP id j5so5978375bkw.2
for <[email protected]>; Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=date:from:to:message-id:subject:mime-version:content-type;
bh=Xj44O9fBIzirvcquOGxJbYLtbqgBc2Ags7prNUuAtSQ=;
b=WWTCdrFgs3RCT/g2qXHR0fLCTc73TmNMA15sff0oIDksB6Nn3IwTYAqTVmoGiNrkW5
08WDpTRADEKAOvjQ5FC9/uBCh1RXWTjxtawfjHc7vfUpqKbXOCj8Ab6GWXQMmX/+WB6T
KVYLhk3/+GddIJI1XsAX9zprSYVcP6MMJ5/U+idIDlC7xQGGNuvzvpAnlnlGuWzgKc6j
qPoFSAAaio6zICY9uSaI0deBIYTEQ2hIuBDJG8oaRvGVvpjhzJaBK+ab+rPJEboHPg8S
3WSCG/Pp212VOw/YXOLUQV0jmMbuqAbsGdeER+Okbwe11sWi+zvPz+jhplB0NB2wq0Fn
mWzA==
Received: by 10.204.130.13 with SMTP id q13mr4084640bks.128.1334664478358;
Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
Return-Path: <[email protected]>
Received: from Akoya ([xx])
by mx.google.com with ESMTPS id z14sm37467763bky.15.2012.04.17.05.07.51
(version=SSLv3 cipher=OTHER);
Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
Date: Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
From: BP MM <[email protected]>
To: xx xx <[email protected]>
Message-ID: <404745073.01334664469900.JavaMail.xx@Akoya>
Subject: TestImage
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_2_854532868.1334664469871"
------=_Part_2_854532868.1334664469871
Content-Type: multipart/mixed;
boundary="----=_Part_1_1457048287.1334664469871"
------=_Part_1_1457048287.1334664469871
Content-Type: image/png; name=logo.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=logo.png
Content-ID: img_cid
[base64 data]
------=_Part_1_1457048287.1334664469871--
------=_Part_2_854532868.1334664469871
Content-Type: multipart/alternative;
boundary="----=_Part_0_1033690582.1334664469858"
------=_Part_0_1033690582.1334664469858
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hello
------=_Part_0_1033690582.1334664469858
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html>
<body>
<H1>Hello</H1>
<img src="cid:img_cid">
</body>
</html>
------=_Part_0_1033690582.1334664469858--
------=_Part_2_854532868.1334664469871--
I've tried different CID's, adding < and > to the CID, setting disposition to inline, validated HTML etc. Can't get this to work, Thunderbird and Gmail view the mail with 'dead' images. I've read that the client looks for the cid in the mail, so I figuered it should fetch the image correctly. Sending the attachment does work, but embedding doesn't. Anyone that has a clue? Is it related to the Multipart structure?
我尝试了不同的 CID,将 < 和 > 添加到 CID,将处置设置为内联,验证 HTML 等。无法让它工作,Thunderbird 和 Gmail 查看带有“死”图像的邮件。我读到客户端在邮件中查找 cid,所以我认为它应该正确获取图像。发送附件确实有效,但嵌入无效。任何有线索的人?和Multipart结构有关系吗?
The java code for creating the message (i've commented some lines trying to solve it..)
用于创建消息的 Java 代码(我已经评论了一些试图解决它的行......)
/*
* The method to create a MimeMessage, this message can be sent with the transport method.
*/
private Message createMessage(final Mail mail, final Member member, final MailingList list,
final MailAccount mailAccount, final Session session)
throws MessagingException, UnsupportedEncodingException {
// Get attachments
@SuppressWarnings("unchecked")
List<File> attachments = (List<File>) mail.getProperty(Mail.ATTACHMENTS_KEY);
// Create content parts
String plainContent = templateEngine.replaceTemplateTags(mail, member, list, mailAccount);
String htmlContent = templateEngine.replaceHTMLTemplateTags(mail, member, list, mailAccount);
Multipart multiContentPart = new MimeMultipart("alternative");
// Multipart attachmentsBodyPart = new MimeMultipart();
Multipart rootBodyPart = new MimeMultipart();
// Create plain text part
if (!plainContent.equals("")) {
BodyPart plainMessageBodyPart = new MimeBodyPart();
plainMessageBodyPart.setContent(plainContent, "text/plain");
multiContentPart.addBodyPart(plainMessageBodyPart);
}
// Create html part
if (!htmlContent.equals("")) {
BodyPart htmlMessageBodyPart = new MimeBodyPart();
htmlMessageBodyPart.setContent(htmlContent, "text/html");
multiContentPart.addBodyPart(htmlMessageBodyPart);
}
// Create attachments
if (attachments != null && attachments.size() > 0) {
for (int i = 0; i < attachments.size(); i++) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachments.get(i));
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachments.get(i).getName());
attachmentBodyPart.setHeader("Content-ID", "<img_cid>");
attachmentBodyPart.setDisposition(Part.INLINE);
// attachmentsBodyPart.addBodyPart(attachmentBodyPart);
rootBodyPart.addBodyPart(attachmentBodyPart);
}
// Build attachments
// BodyPart attachmentsWrapper = new MimeBodyPart();
// attachmentsWrapper.setContent(attachmentsBodyPart);
// rootBodyPart.addBodyPart(attachmentsWrapper);
}
// Build content
BodyPart contentWrapper = new MimeBodyPart();
contentWrapper.setContent(multiContentPart);
rootBodyPart.addBodyPart(contentWrapper);
// Create message
Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
member.getEmail(), member.getName()));
message.setSubject((String) mail.getProperty(Mail.SUBJECT_KEY));
message.setContent(rootBodyPart);
// Add headers
message.setFrom(new InternetAddress(mailAccount.getFromAddress(),
mailAccount.getFromName()));
if (mailAccount.getReplyTo() != "" && mailAccount.getReplyTo() != null)
message.setReplyTo(InternetAddress.parse(mailAccount.getReplyTo()));
return message;
}
Don't mind the CID being the same for each attachment, later this will be the file name - I'm only testing with a single attachment for now. Hope this helps..
不要介意每个附件的 CID 相同,稍后这将是文件名 - 我现在只测试一个附件。希望这可以帮助..
采纳答案by krobbens
I found that there wasn't anything wrong with the mail source. It's the way mailclients handle CIDs. Gmail.com requires the CID to be set between < and >. Thunderbird hasn't shown my images yet.. I think it needs to be unique of some sort.. Still haven't figured why and how to fix it. Found it through Embedding images into html email with java mail
我发现邮件源没有任何问题。这是邮件客户端处理 CID 的方式。Gmail.com 要求在 < 和 > 之间设置 CID。Thunderbird 还没有显示我的图像......我认为它需要某种独特的......仍然没有弄清楚为什么以及如何修复它。通过Embedding images into html email with java mail 找到的
回答by rampraveen
CID is working correctly , problem is when you are sending mail to mutilple email id , you have to initlize MimeMessage every time the you will not get error
CID 工作正常,问题是当您向多个电子邮件 ID 发送邮件时,您必须每次都初始化 MimeMessage 不会出错
MimeMessage m = new MimeMessage(mailSession);
MimeMessage m = 新 MimeMessage(mailSession);
回答by Luis Bartolessi
It seems incredible, but all html tags must be on the same line. Between '<' and '>' there can be no carriage return. The parser is very seedy.
看起来不可思议,但是所有的 html 标签都必须在同一行上。'<' 和 '>' 之间不能有回车。解析器非常破旧。