有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用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 );,但仍然没有成功


共 (2) 个答案

  1. # 1 楼答案

    您需要创建一个多部分/相关消息。{a1}有一个例子

  2. # 2 楼答案

    研究如何使用mimessagehelper。这会让你的生活更轻松

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