【问题标题】:Sending mail along with embedded image using javamail使用 javamail 发送邮件和嵌入的图像
【发布时间】:2010-11-01 17:33:27
【问题描述】:

我想连同嵌入的图片一起发送邮件。为此,我使用了以下代码。它不是完整的代码。它是代码的一部分

        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);

我面临的问题是,我可以收到邮件但无法看到图像.. 它没有显示在邮件中。
以下是我的部分 html 文件

             <img src=\"cid:BarcodeImage\" alt="Barcode" width="166" height="44" align="right" />

请帮助我为什么图像没有显示在邮件中以及为什么它不在附件中??

【问题讨论】:

  • 您是否检查过添加附件块是否被调用,即。该文件存在吗?
  • 是的它正在执行..我已经检查了sop行n检查..那个块正在执行。

标签: java email jakarta-mail


【解决方案1】:

尝试删除以下行:

messageBodyPart.setDisposition("inline");

【讨论】:

  • 更通用的方法是messageBodyPart.setDisposition(MimeBodyPart.INLINE)
【解决方案2】:

new MimeMultipart("related"); 更改为new MimeMultipart();(也可选择将msg.setContent(multipart); 更改为msg.setContent(multipart,"multipart/related");) 还要确保将img src=\"cid:BarcodeImage\" 更改为img src="cid:BarcodeImage"。 那么它应该可以工作了。

【讨论】:

    【解决方案3】:

    我偶然发现了类似的问题。 以下帖子对我帮助很大: How to send email with embedded images using 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);
    

    函数 generateCID() 必须返回唯一的字符串。 例如:

    java.util.UUID.randomUUID()
    

    【讨论】:

      【解决方案4】:

      将“相对”更改为“替代”,然后您将获得图像作为附件。

          Multipart multipart = new MimeMultipart("alternative");
      

      【讨论】:

        猜你喜欢
        • 2010-11-09
        • 2011-03-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-12
        • 2013-04-27
        • 2015-12-17
        相关资源
        最近更新 更多