1

目前我们收到一封被解析的电子邮件

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

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

4

2 回答 2

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
}
于 2020-06-26T11:14:21.377 回答
0

答案是 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;
}
于 2018-09-21T20:20:41.230 回答