提取包含邮件的 PST 文件作为 PSTMessage 的附件

Extracting PST file containing mail as an attachment of PSTMessage

我正在尝试使用 java-libpst-0.8.1 提取 PST 文件,如下所示 https://code.google.com/p/java-libpst/

在我的示例 pst 文件中,有几封邮件。在那个pst文件的其中一封邮件中,附件也是一封邮件。在解析 PSTMessage 时,它甚至无法获取附件名称。请找到示例代码。

 PSTMessage email;
 PSTAttachment attach;
 email = (PSTMessage) folder.getNextChild();
 while (email != null) {
    try {
        numberOfAttachments = email.getNumberOfAttachments();
        if (numberOfAttachments > 0) {
           for (int x = 0; x < numberOfAttachments; x++) {
               attach = email.getAttachment(x);
               try {
                    attachmentName = attach.getLongFilename();

虽然程序给出了邮件附件的确切数量。但它无法提供附加邮件的名称或提取其内容。谁能建议我该怎么做?

JPST 将嵌入的消息导出为原始 .msg 文件格式的附件。我记得他们在附件对象中有方法来识别附件是否是嵌入的消息。

JPST 是另一个库,不是免费的,但值得一试。

示例:

for (Attachment attachment : message.getAttachments()) {
    Message embeddedMessage = attachment.getEmbeddedMessage();
    if (embeddedMessage != null) {
        embeddedMessage.setEmbedded(false); //Important
        embeddedMessage.save("e:\testfolder\" +
            getFileName(attachment.getDisplayName()) + ".msg");
    } else {
        attachment.save("e:\testfolder\" + attachment.getFileName());
    }
}

终于,我能够阅读作为邮件附件的邮件了。 PSTAttachmentclass中有方法getEmbeddedPSTMessage()。首先我需要检查它是普通附件还是邮件。为此,我们需要参考 getAttachMethod()。如果它 returns 5 它是一个嵌入的消息。详情请查看documentation of PSTAttachment.

if (attach.getAttachMethod() == 5) {
     PSTMessage attachEmail = attach.getEmbeddedPSTMessage();
}

您是否尝试过将 EmbeddedPSTMessage 转换为实际的 .msg 格式?

我正在使用库读取 pst 文件,第二个过程是我正在将 PSTMessage 转换为 javax.mail.internet.MimeMessage 并将其另存为 .eml。

我现在遇到的问题是,只要附件是嵌入式消息(.msg 格式),它就会转换为 .dat 扩展名。

您知道如何转换 EmbeddedPSTMessage 并将其附加为原始消息格式吗?

我现在真的很绝望

下面是代码片段:

// saving as .eml
MimeMessage mimeMessage = convertToMimeMessage(email);
fileName = getRidOfIllegalFileNameCharacters(email.getSubject());
File emlFile = new File("C:\eml\" + fileName + ".eml");
emlFile.createNewFile();
mimeMessage.writeTo(new FileOutputStream(emlFile));

private static MimeMessage convertToMimeMessage(PSTMessage email) throws MessagingException, IOException, PSTException {
  Properties p = System.getProperties();
  Session session = Session.getInstance(p);
  MimeMessage mimeMessage = new MimeMessage(session);

  //attachment part
  MimeMultipart rootMultipart = new MimeMultipart();
  for (int i = 0; i < email.getNumberOfAttachments(); i++) {
    PSTAttachment attachment = email.getAttachment(i);

    if (attachment != null && attachment.getFileInputStream() != null) {
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();

        if (attachment.getMimeTag() != null && attachment.getMimeTag().length() > 0) {
            DataSource source = new ByteArrayDataSource(attachment.getFileInputStream(), attachment.getMimeTag());
            attachmentBodyPart.setDataHandler(new DataHandler(source));
        } else {
            DataSource source = new ByteArrayDataSource(attachment.getFileInputStream(), "application/octet-stream");
            attachmentBodyPart.setDataHandler(new DataHandler(source));
        }
        attachmentBodyPart.setContentID(attachment.getContentId());

        String fileName = "";

        if (attachment.getLongFilename() != null && attachment.getLongFilename().length() > 0) {
            fileName = attachment.getLongFilename();
        } else if (attachment.getDisplayName() != null && attachment.getDisplayName().length() > 0) {
            fileName = attachment.getDisplayName();
        } else if (attachment.getFilename() != null && attachment.getFilename().length() > 0) {
            fileName = attachment.getFilename();
        }

        attachmentBodyPart.setFileName(fileName);

        rootMultipart.addBodyPart(attachmentBodyPart);
    }
}

mimeMessage.setContent(rootMultipart);
return mimeMessage;