【发布时间】:2012-10-20 20:46:20
【问题描述】:
我正在发送带有 mime 消息的电子邮件的内联图像。这是相同的简短代码。这工作正常。 我的 问题是我没有将 MimeMessage 内容类型设置为多部分/相关(也没有设置 相关的多部分子类型)我的代码仍然可以正常工作,并且能够在预期位置获得内联图像。 当我用 cid 引用图像部分时,我是否真的关心将 Content-Type 设置为 multipart/related 还是服务器负责处理?
MimeMessage msg = new MimeMessage(mailSession);
MimeMultipart mpart = new MimeMultipart();
MimeBodyPart bp = new MimeBodyPart();
bp.setText("plain text and here is html image refering image part <img src="cid:Unique-ContentId" />", CHARSET_UTF_8, MESSAGE_HTML_CONTENT_TYPE);
// add message body
mpart.addBodyPart(bp);
// adding inline image part
MimeBodyPart bodyPart1 = new MimeBodyPart();
bodyPart1.setFileName("inline image");
file1 = new File("image1");
DataSource source1 = new FileDataSource(file);
bodyPart1.setDataHandler(new DataHandler(source));
bodyPart1.setDisposition(MimeBodyPart.INLINE);
bodyPart1.setHeader("Content-ID", "Unique-ContentId");
bodyPart1.setHeader("Content-Type", "image/jpeg");
mpart.addBodyPart(bodyPart1);
// At last setting multipart In MimeMessage
msg.setContent(mpart);
仅供参考,我的电子邮件客户端可以是 Outlook、lotusnotes、yahoo、gmail、thunderbird
【问题讨论】:
标签: java jakarta-mail mime-types mime-message