You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Apache POI的MAPIMessage提取MSG文件中嵌入式图片的CID?

获取MSG内嵌附件的CID(基于Apache POI)

要提取内嵌附件的CID,核心是遍历每个AttachmentChunks的子块,匹配对应MAPI属性的标签或名称:

实现代码

MAPIMessage message = new MAPIMessage(fileInputStream);
AttachmentChunks[] attachments = message.getAttachmentFiles();

for (AttachmentChunks attachment : attachments) {
    MAPIChunk[] chunks = attachment.getChunks();
    for (MAPIChunk chunk : chunks) {
        // 匹配 PR_ATTACH_CONTENT_ID 属性的标签(0x3712001F)
        if (chunk.getTag() == 0x3712001F) {
            String cid = chunk.getString();
            // 这里可以处理获取到的CID,比如关联到对应附件
            System.out.println("附件CID: " + cid);
            break;
        }
    }
}

说明

CID对应的MAPI标准属性是PR_ATTACH_CONTENT_ID,其标签值固定为0x3712001F。通过遍历附件的所有子块,匹配该标签即可直接获取CID字符串,全程无需新增额外依赖,完全基于Apache POI的现有API。

如果标签值匹配存在疑问,也可以通过判断子块的名称来匹配:

if ("__substg1.0_3712001F".equals(chunk.getEntryName())) {
    String cid = chunk.getString();
    // 处理CID
}

内容的提问来源于stack exchange,提问作者esh_08

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:32:37