📅  最后修改于: 2023-12-03 14:59:20.796000             🧑  作者: Mango
Apache POI MSG 解析器是一个用于解析 Microsoft Outlook 邮件文件(.msg)的 Java 库。
在 Maven 项目中添加以下依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
import org.apache.poi.hsmf.datatypes.MessageHeader;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MsgParser {
public static void main(String[] args) throws Exception {
String filePath = "/path/to/file.msg";
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
MAPIMessage msg = new MAPIMessage(fs);
MessageHeader header = msg.getMessageHeader();
System.out.println("From: " + header.getFromDisplayName());
System.out.println("Subject: " + header.getSubject());
System.out.println("Sent on: " + header.getDeliveryDate());
List<AttachmentChunks> attachments = msg.getAttachmentFiles();
if (!attachments.isEmpty()) {
System.out.println("Attachments:");
for (AttachmentChunks attachment : attachments) {
System.out.println("- " + attachment.attachLongFileName);
}
}
String text = msg.getTextBody();
if (text != null) {
System.out.println("Text body:");
System.out.println(text);
}
List<MAPIMessage> recipients = msg.getRecipientDetails();
if (!recipients.isEmpty()) {
System.out.println("Recipients:");
for (MAPIMessage recipient : recipients) {
System.out.println("- " + recipient.getRecipientFullName() + " (" + recipient.getRecipientEmailAddress() + ")");
}
}
}
}
Apache POI MSG 解析器是一个非常强大和灵活的工具,可以帮助开发人员解析和处理 MSG 文件。它的使用也相对简单,只需要引入依赖并使用 API 即可轻松完成 MSG 文件的解析。