【问题标题】:Extract inline email attachment with Java使用 Java 提取内联电子邮件附件
【发布时间】:2018-04-11 00:06:53
【问题描述】:

我正在使用以下Java 代码来尝试提取电子邮件附件:

private static List<File> extractAttachment(Message message) {
    List<File> attachments = new ArrayList<File>();
    try {
        Multipart multipart = (Multipart) message.getContent();

        for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
        if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
            continue; // dealing with attachments only
        }
        InputStream is = bodyPart.getInputStream();
        String filePath = "/tmp/" + bodyPart.getFileName();
        System.out.println("Saving: " + filePath);
        File f = new File(filePath);
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buf)) != -1) {
            fos.write(buf, 0, bytesRead);
        }
        fos.close();
        attachments.add(f);
        }
    } catch (IOException | MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return attachments;
 }

但是,我总是收到bodyPart.getDisposition(): null。任何线索我应该如何提取内联附件?

谢谢

P.S.:我在我的 Mac 上使用 Apple Mail 客户端发送带有附件的测试电子邮件。但是电子邮件客户端不应该受到关注。

【问题讨论】:

    标签: java jakarta-mail email-attachments apple-mail


    【解决方案1】:

    当从没有正文内容但附件的 mac 发送消息时,我遇到了类似的问题,在迭代初始消息内容时附件未显示,并且还必须递归查找。

    我在下面发布了对我有用的内容。

    private Map<String, String> extractAttachments(Multipart multiPart, Map<String, String> attachmentAndName) {
        try {
            for (int i = 0; i < multiPart.getCount(); i++) {
                BodyPart part = multiPart.getBodyPart(i);
                if (part.getContent() instanceof Multipart) {
                    attachmentAndName = extractAttachments((Multipart) part.getContent(), attachmentAndName);
                } else if (part instanceof MimeBodyPart) {
                    MimeBodyPart mimeBodyPart = (MimeBodyPart) part;
                    String fileName = createUniqueFileName(mimeBodyPart.getFileName());
                    if (Part.ATTACHMENT.equalsIgnoreCase(mimeBodyPart.getDisposition())) {
                        String attachmentAsString = IOUtils.toString(mimeBodyPart.getInputStream(), StandardCharsets.UTF_8);
                        attachmentAndName.put(fileName, attachmentAsString);
                        if (SAVE_ATTACHMENTS_LOCALLY) {
                            saveFile(mimeBodyPart, fileName);
                        }
                    }
                }
            }
        } catch (IOException | MessagingException e) {
            logger.error("Failed to process attachment. Continuing to process other message parts in search of attachments...");
        }
        return attachmentAndName;
    }
    

    【讨论】:

      【解决方案2】:

      我明白了,确实我不得不进行一些“递归”并检查嵌套内容并检查Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition()

      以下是代码,认为对某人有用:

      private static List<File> extractAttachment(Multipart multipart) {
          List<File> attachments = new ArrayList<File>();
          try {
      
              for (int i = 0; i < multipart.getCount(); i++) {
              BodyPart bodyPart = multipart.getBodyPart(i);
      
              if (bodyPart.getContent() instanceof Multipart) {
                  // part-within-a-part, do some recursion...
                  extractAttachment((Multipart) bodyPart.getContent());
              }
      
              System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
              if (!Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition())) {
                  continue; // dealing with attachments only
              }
      
              InputStream is = bodyPart.getInputStream();
              String filePath = "/tmp/" + bodyPart.getFileName();
              System.out.println("Saving: " + filePath);
              File f = new File(filePath);
              FileOutputStream fos = new FileOutputStream(f);
              byte[] buf = new byte[4096];
              int bytesRead;
              while ((bytesRead = is.read(buf)) != -1) {
                  fos.write(buf, 0, bytesRead);
              }
              fos.close();
              attachments.add(f);
              }
          } catch (IOException | MessagingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return attachments;
          }
      

      【讨论】:

        猜你喜欢
        • 2017-02-18
        • 2015-08-01
        • 2012-03-24
        • 2015-03-29
        • 2017-11-13
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多