代码之家  ›  专栏  ›  技术社区  ›  StealthRT

Java在电子邮件正文中嵌入多个图像

  •  0
  • StealthRT  · 技术社区  · 7 年前

    嘿我只是想 在我的电子邮件正文中嵌入图像 将在中查看 展望2016 .

    多个图像嵌入到身体中 那么我该怎么做呢?

    <h1>我有一些附件给你。%img</h1>

    其中 被替换一个 图像cid名称

    当前的代码 只有一个嵌入图像

    private static boolean createEmbeddedImg(MimeBodyPart messageBodyPart, Multipart multipart) throws MessagingException, IOException {
            int seq = 0;
            int c = (seq++) % 100000;
    
            String cid = c + "." + System.currentTimeMillis();
    
            messageBodyPart.setText(""
                      + "<html>"
                      + " <body>"
                      + "  <p>Here is my image:</p>"
                      + "  <img src=\"cid:" + cid + "\" />"
                      + " </body>"
                      + "</html>" 
                      ,"US-ASCII", "html");
            multipart.addBodyPart(messageBodyPart);
    
            MimeBodyPart imagePart = new MimeBodyPart();
    
            try {
                byte[] decodedImg = Base64.getDecoder().decode(B64.getBytes(StandardCharsets.UTF_8));
                Path destinationFile = Paths.get("c:/temp", "homer.gif");
                Files.write(destinationFile, decodedImg);
            } catch (IOException e) {
                e.printStackTrace();
                 return false;
            }
    
            imagePart.attachFile("c:/temp/homer.gif");
            imagePart.setContentID("<" + cid + ">");
            imagePart.setDisposition(MimeBodyPart.INLINE);
            multipart.addBodyPart(imagePart);
            multipart.addBodyPart(messageBodyPart);
    
            return true;
        }
    

    上面的代码生成一封包含嵌入图像和正文消息的电子邮件:

    enter image description here

    下面这段代码是我想出的 不止一幅嵌入式图像 在身体里:

    private static void _createEmbeddedImgs(MimeBodyPart messageBodyPart, Multipart multipart, String message,
            String[] embeddedImgs) throws MessagingException, IOException {
        UUID uuid = UUID.randomUUID();
        String cid = null;
        List<String> savedCIDS = new ArrayList<String>();
    
        if (embeddedImgs != null && embeddedImgs.length > 0) {
            for (String filePath : embeddedImgs) {
                cid = String.valueOf(uuid.variant());
                message = message.replaceFirst("%img", "<img src=\"cid:" + cid + "\" />");
    
                MimeBodyPart imagePart = new MimeBodyPart();
    
                byte[] decodedImg = Base64.getDecoder().decode(B64.getBytes(StandardCharsets.UTF_8));
                Path destinationFile = Paths.get("c:/temp", cid + ".gif");
    
                try {
                    Files.write(destinationFile, decodedImg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                imagePart.attachFile("c:/temp/" + cid + ".gif");
                imagePart.setContentID("<" + cid + ">");
                imagePart.setDisposition(MimeBodyPart.INLINE);
                multipart.addBodyPart(imagePart);
                savedCIDS.add(String.valueOf(cid));
            }
    
            messageBodyPart.setText("<html><body>" + message + "</html></body>", "US-ASCII", "html");
            multipart.addBodyPart(messageBodyPart);
        }
    }
    

    这确实会生成一封电子邮件,但看起来是这样的:

    enter image description here

    所以身体里什么都没有,只有它有嵌入的图像(2.gif)

    我似乎无法描绘出我所缺少的东西,以便它也能像我所期望的那样工作。

    更新

    enter image description here

    牧师代码已更改:

    int seq = 0;
            int c = (seq++) % 100000;
    
            String cid = c + "." + System.currentTimeMillis();
    
            if (embeddedImgs != null && embeddedImgs.length > 0) {
                for (String filePath : embeddedImgs) {
                    message = message.replaceFirst("%img", "<img src=\"cid:" + cid + "\" />");
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Bill Shannon    7 年前

    JavaMail FAQ

    您需要创建一个多部分/相关消息,其中文本作为第一部分,图像作为后面的部分。

    请注意,在将图像数据包含在消息中之前,不需要将其写入文件;有关详细信息,请参见常见问题解答。

        2
  •  0
  •   drkunibar    7 年前

    您可以直接将图像插入到HTML中

        messageBodyPart.setText(""
                  + "<html>"
                  + " <body>"
                  + "  <p>Here is my image:</p>"
                  + "  <img src=\"data:image/jpg;base64," + Base64.getEncoder().encodeToString(YOUR_IMAGE_DATA) + "\" />"
                  + " </body>"
                  + "</html>" 
                  ,"US-ASCII", "html");
        multipart.addBodyPart(messageBodyPart);
    

    如果不使用JPEG,请更改图像的mimetype。

    推荐文章