我正在尝试使用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();
虽然程序正在给出邮件附件的确切数量。但它无法提供附加邮件的名称或提取其内容。任何人都可以建议我该怎么做?
答案 0 :(得分:4)
最后,我能够阅读邮件的附件邮件。 getEmbeddedPSTMessage()
类中有一个方法PSTAttachment
。首先,我需要检查它是正常附件还是邮件。为此,我们需要参考getAttachMethod()
。如果它返回5
,则它是嵌入的消息。有关详细信息,请查看documentation of PSTAttachment
。
if (attach.getAttachMethod() == 5) {
PSTMessage attachEmail = attach.getEmbeddedPSTMessage();
}
答案 1 :(得分:3)
您是否尝试将EmbeddedPSTMessage转换为实际的.msg格式?
我正在使用该库来读取pst文件,第二个过程是我将PSTMessage转换为javax.mail.internet.MimeMessage并将其保存为.eml。
我现在遇到的问题是,只要附件是嵌入式消息(.msg格式),它就会转换为.dat扩展名。
您是否知道如何转换EmbeddedPSTMessage并将其作为原始的msg格式附加?
我现在非常绝望。
以下是代码段:
// 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;
答案 2 :(得分:0)
JPST以原始.msg文件格式将嵌入式邮件作为附件导出。我记得他们在Attachment对象中有方法来识别附件是否是嵌入消息。
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());
}
}