java 使用javamail发送带有嵌入图像的邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/974196/
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
Sending mail along with embedded image using javamail
提问by
I want to send mail along with embedded image. For that i have used the below code. Its not full code. Its a part of code
我想与嵌入的图像一起发送邮件。为此,我使用了以下代码。它不是完整的代码。它是代码的一部分
Multipart multipart = new MimeMultipart("related");
// Create the message part
BodyPart messageBodyPart;
messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(msgBody); // msgbody contains the contents of the html file
messageBodyPart.setHeader("Content-Type", "text/html");
multipart.addBodyPart(messageBodyPart);
//add file attachments
DataSource source;
File file = new File("D:/sample.jpeg");
if(file.exists()){
// add attachment
messageBodyPart = new MimeBodyPart();
source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
messageBodyPart.setHeader("Content-ID", "<BarcodeImage>");
messageBodyPart.setDisposition("inline");
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
msg.setContent(multipart);
Transport.send(msg);
Problem i am facing is, i can get the mail but cant acle to see the image.. Its not get displaying in the mail.
Below is my part of html file
我面临的问题是,我可以收到邮件,但无法查看图像.. 它没有显示在邮件中。
下面是我的 html 文件的一部分
<img src=\"cid:BarcodeImage\" alt="Barcode" width="166" height="44" align="right" />
Please help me why the image not getting displayed in the mail and why it is not in the attachment??
请帮助我为什么图像没有显示在邮件中,为什么它不在附件中?
回答by DominikStyp
I've stumbled on the similar problem. Following post helped me a lot: How to send email with embedded images using JavaThe most important part of the code is:
我偶然发现了类似的问题。以下帖子对我帮助很大: 如何使用 Java 发送带有嵌入图像的电子邮件代码中最重要的部分是:
String cid = generateCID();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("<html><head>"
+ "<title>This is not usually displayed</title>"
+ "</head>n"
+ "<body><div><strong>Hi there!</strong></div>"
+ "<div>Sending HTML in email is so <em>cool!</em> </div>n"
+ "<div>And here's an image: <img src=\"cid:\"" + cid + " /></div>"
+ "<div>I hope you like it!</div></body></html>",
"US-ASCII", "html");
content.addBodyPart(textPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("resources/teapot.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
Function generateCID() must return unique String. For example:
函数 generateCID() 必须返回唯一的字符串。例如:
java.util.UUID.randomUUID()
回答by teja
Change the "relative" to "alternative", then you will get the image as an attachment.
将“相对”更改为“替代”,然后您将获得图像作为附件。
Multipart multipart = new MimeMultipart("alternative");
回答by objects
Try getting rid of the following line:
尝试摆脱以下行:
messageBodyPart.setDisposition("inline");
回答by SWdV
Change new MimeMultipart("related");to new MimeMultipart();(and optionally msg.setContent(multipart);to msg.setContent(multipart,"multipart/related");)
Also make sure you change img src=\"cid:BarcodeImage\"to img src="cid:BarcodeImage".
It should work then.
更改new MimeMultipart("related");到new MimeMultipart();(和可选msg.setContent(multipart);到msg.setContent(multipart,"multipart/related");)另外,请确保您更改img src=\"cid:BarcodeImage\"到img src="cid:BarcodeImage"。它应该工作。

