当前位置: 首页 > 知识库问答 >
问题:

用java下载包含内联图像的电子邮件正文

双恩
2023-03-14

我的问题如下:

    Void readMessages(Folder folder){

          Message[] messages = folder.getMessages();
            // loading of message objects.
                for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {

             final Message currentMessage = messages[messageNumber];
                 logger.info("Handling the mail with subject " + currentMessage.getSubject());
                logger.info("Content type for the current message is " +                                  currentMessage.getContentType());
                final String messageFileName = currentMessage.getFileName();
                logger.info("File name for the message " + messageFileName + ". File name is blank "
                                                +                     StringUtils.isBlank(messageFileName));


                        Object messageContentObject = currentMessage.getContent();
                        if (messageContentObject instanceof Multipart) {
                            Multipart multipart = (Multipart) messageContentObject;

                            // downloading all attachments....
                            int attachmentCount = multipart.getCount();
                            logger.info("Number of attachments ");
                            for (int i = 0; i < attachmentCount; i++) {
                                Part part = (Part) multipart.getBodyPart(i);
                                downloadAttachment(part, folderPath.toString());
                            }

                        }

                    }
                }
            }
         private void downloadAttachment(Part part, String folderPath) throws Exception {
    String disPosition = part.getDisposition();
    String fileName = part.getFileName();
    String decodedText = null;
    logger.info("Disposition type :: " + disPosition);
    logger.info("Attached File Name :: " + fileName);

    if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
        logger.info("DisPosition is ATTACHMENT type.");
        File file = new File(folderPath + File.separator + decodedText);
        file.getParentFile().mkdirs();
        saveEmailAttachment(file, part);
    } else if (fileName != null && disPosition == null) {
        logger.info("DisPosition is Null type but file name is valid.  Possibly inline attchment");
        File file = new File(folderPath + File.separator + decodedText);
        file.getParentFile().mkdirs();
        saveEmailAttachment(file, part);
    } else if (fileName == null && disPosition == null) {
        logger.info("DisPosition is Null type but file name is null. It is email body.");
        File file = new File(folderPath + File.separator + "mail.html");
        file.getParentFile().mkdirs();
        saveEmailAttachment(file, part);
    }


}
     protected int saveEmailAttachment(File saveFile, Part part) throws Exception {

    BufferedOutputStream bos = null;
    InputStream is = null;
    int ret = 0, count = 0;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        part.writeTo(new FileOutputStream(saveFile));

    } finally {
        try {
            if (bos != null) {
                bos.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            logger.error("Error while closing the stream.", ioe);
        }
    }
    return count;
} 
 File file = new File(folderPath + File.separator + "mail.html"); 
 File file = new File(folderPath + File.separator + "mail.eml");

共有1个答案

林鸿飞
2023-03-14

我写了以下代码转换电子邮件正文到pdf包括内联图像。在代码中,我用下载图像路径替换了图像代码(ex:cid:image001.jpg@01d17aaa.1ea2a6a0)。我正在为图像键和下载路径构建“HashMap”,同时下载图像。

 HTMLWorker htmlWorker = new HTMLWorker(document);
            if(bodyStr!=null)
            {

                //find inline images
                inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory);
                if(inlineImages!=null)
                {

                    for (Map.Entry<String, String> entry : inlineImages.entrySet()) {
                        //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                        bodyStr=bodyStr.replaceAll("cid:"+entry.getKey() , entry.getValue());
                    }
                }
                htmlWorker.parse(new StringReader(bodyStr));

        }

下载带有传递项的内联图像。

 private HashMap<String,String> downloadInLineImage(Item item, String dynamicOutputDirectory)
        throws Exception, ServiceLocalException {
    //create output directory if not present

        //bind the item to a new email message. if you do not bind, then the getHasAttachments() function will fail
    EmailMessage mostRecentMatch = (EmailMessage)item;
    String from = mostRecentMatch.getFrom().getAddress();
    String user =StringUtils.substringBefore(from, "@");
    AttachmentCollection collection=item.getAttachments();

    HashMap<String,String> inlineFiles=new HashMap<String,String>();

    if(collection.getCount()>0)
    {
        for (Attachment attachment : collection.getItems()) {

            if(attachment.getIsInline())
            {

                FileAttachment currentFile = (FileAttachment) attachment;
                String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName();
                File file=new File(filePath);
                FileOutputStream fio=new FileOutputStream(file);
                currentFile.load(fio);
                inlineFiles.put(currentFile.getContentId(), filePath);
                fio.close();
            }
        }
    }
 类似资料:
  • 我正在使用spring Boot从java发送HTML电子邮件。电子邮件包括签名与我们公司的形象标志。它工作得很好。在Gmail上。但在MacOS应用程序电子邮件中,徽标是作为附件发送的,而不是内联的。 代码的非相关部分替换为...

  • 问题内容: 我想使用go lang在电子邮件正文中发送图像。从 https://github.com/scorredoira/email 现在,我可以将图像文件作为附件发送,但是我需要在电子邮件正文中发送图像文件。 提前致谢。 问题答案: 您可以使用Gomail(我是作者)。看一下Embed方法,它允许您将图像嵌入到电子邮件正文中:

  • 我正在尝试在电子邮件中嵌入一个图像。我遵循了这里,这里和这里的例子和其他但我不能得到图像显示。 当我执行这个时,我得到的是一个空空如也的身体,里面有一个红十字,没有任何图像。如何使图像与电子邮件正文内联显示? 我正在使用Outlook 2016。我知道我可以在使用Outlook时插入图片,我也收到过别人在文本中插入图片的“正常”电子邮件,所以这肯定意味着我必须能够查看python脚本生成的图片?

  • 我想提供一个功能,用户可以审查他们上传的所有图像。在我的firebase存储中,它包含文件夹中的用户映像。 但是,我不知道如何获得完整用户文件的引用。我尝试了下面的代码来下载它,但是我得到了一个错误代码。 我只想获得一个列表的图像的下载url的Json格式。例如,

  • 问题内容: 注意:我不想将图像附加到电子邮件 我想在电子邮件正文中显示图片, 我已经尝试了HTML image标签,并且得到了输出,正如您在此中看到的关于如何在电子邮件正文中添加图像的问题一样,我很累。 它对我不起作用,它也给我相同的输出,所以我怀疑是否可以这样做, 我的密码 更新1: 如果我使用该代码,则可以获取文本和图像,但无法在电子邮件正文中看到图像 这是我的代码: 更新2: 我使用了粗体标