【问题标题】:How to add a inline image using javamail?如何使用 javamail 添加内联图像?
【发布时间】:2019-05-20 15:43:56
【问题描述】:

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

我可以将图像添加为内联图像,但它们也出现在附件中,所以我无法避免这个附件。

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 );,但还是不行。

【问题讨论】:

    标签: java spring-boot jakarta-mail


    【解决方案1】:

    您需要创建多部分/相关消息。 JavaMail FAQ 有一个例子。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-08-31
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 2014-12-03
        • 2019-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多