簡體   English   中英

如何在 Apache Commons Email 1.4 中接收和區分常規附件和內聯附件

[英]How to receive and distinguish between regular attachments and inline attachments in Apache Commons Email 1.4

目前我們收到一封電子郵件,它被解析

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 這樣的常規附件,則不能。

我的印象是沒有進入較低級別的解決方法......但我還沒有檢查所有附件類型 - 僅使用圖像。 像這樣的東西:

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 做不到這樣的事情。 您必須在 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM