繁体   English   中英

如何使用javamail添加内嵌图像?

[英]How to add a inline image using javamail?

我想将内嵌图像发送到电子邮件。 我几乎尝试了所有可能的方法,但没有运气。

我能够将图像添加为内联,但它们也出现在附件中,因此我无法避免此附件。

messageBodyPart = new MimeBodyPart();
            String htmlText = "<H1>This is the image: </H1><img src=\"cid:image\">";
            ((MimeBodyPart) messageBodyPart).setText(htmlText, null, "html");
            mp.addBodyPart(messageBodyPart);

            // second part (the image)
            messageBodyPart = new MimeBodyPart();
            String filePath = "abc.png";
            ((MimeBodyPart) messageBodyPart).attachFile(filePath, "image/png", "base64");
            ((MimeBodyPart) messageBodyPart).setContentID("<image>");
            mp.addBodyPart( messageBodyPart );

我也尝试过使用messageBodyPart.setDisposition( MimePart.INLINE ); ,但仍然没有运气。

考虑使用MimeMessageHelper。 它会让你的生活更轻松

MimeMessageHelper helper = new MimeMessageHelper( mimeMessage, true );
helper.setText( htmlText, true );
helper.addInline( "image", signatureImage ); // image here is the cid

您需要创建一个multipart / related消息。 JavaMail FAQ有一个例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM