【问题标题】:How to receive and distinguish between regular attachments and inline attachments in Apache Commons Email 1.4如何在 Apache Commons Email 1.4 中接收和区分常规附件和内联附件
【发布时间】:2019-02-21 17:06:44
【问题描述】:

目前我们收到一封被

解析的电子邮件
MimeMessageParser mimeMessageParser = parse(message);

然后用

拉出附件
 if (mimeMessageParser.hasAttachments()) {
     List<DataSource> attachments = mimeMessageParser.getAttachmentList();
     for (DataSource dataSource : attachments) {
         saveAttachment(dataSource, subjectLineProperties, documentToUpload, firstHeaders);
     }
 }

问题是 getAttachmentList 还返回内联图像,如签名行中的业务徽标,我们不想将内联图像作为附件拉出。我们只需要实际的电子邮件附件。 ATTACHMENT 与 INLINE,但我们也无法通过 Apache Commons Email 1.4 版本访问 java.mail 配置,也找不到解决方案。我检查了他们的文档https://commons.apache.org/proper/commons-email/javadocs/api-1.4/index.html

运气不好。似乎附件 DataSource 只允许我获取内容和内容类型和名称,但如果它是内联附件/图像或像 Mime Parts 这样的常规附件则不能。

【问题讨论】:

    标签: apache-commons-email


    【解决方案1】:

    我的印象是有一种解决方法,无需进入较低级别...但我尚未检查所有附件类型 - 仅检查图像。像这样的:

    for(DataSource ds : mimeMessageParser.getAttachmentList()) {
        for(String id : mimeMessageParser.getContentIds()) {
            if(ds == mimeMessageParser.findAttachmentByCid(id)) {
                // It is inline attachment with Content ID
                break;
            }
        }
        // If not found - it is file attachment without content ID
    }
    

    【讨论】:

    • 感谢安迪的回复。对于我们的情况,由于此低级代码适用于所有附件,因此我们不会更改我们的代码。但我确实记得尝试了 Apache Commons Email 中的所有内容,但没有解决方案。我希望有新人出现并可以尝试您的代码并在可行时发表评论。祝你有美好的一天。
    【解决方案2】:

    答案是 Apache Commons Email 不能做这样的事情。为了做出这些区别,您必须降低级别并在 JDK 中编写老式的 MimeMessage 和 MultiPart 类。

    所以从 mimeMessageParser.getAttachmentList();我们现在有电话了

            if (mimeMessageParser.hasAttachments()) {
                final Multipart mp = (Multipart) message.getContent();
                if (mp != null) {
                    List<DataSource> attachments = extractAttachment(mp);
                    for (DataSource dataSource : attachments) {
                        saveAttachment(dataSource, subjectLineProperties, documentToUpload, firstHeaders);
                    }
                }
            }
    
    
    private static List<DataSource> extractAttachment(Multipart multipart) {
        List<DataSource> attachments = new ArrayList<>();
        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.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                    continue; // dealing with attachments only
                }
    
                InputStream is = bodyPart.getInputStream();
                String fileName = bodyPart.getFileName();
                String contentType = bodyPart.getContentType();
                ByteArrayDataSource dataSource = new ByteArrayDataSource(is, contentType);
                dataSource.setName(fileName);
                attachments.add(dataSource);
            }
        } catch (IOException | MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return attachments;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 2010-12-09
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2015-08-01
      • 2010-11-26
      相关资源
      最近更新 更多