5

I'm trying to extract PST files using java-libpst-0.8.1 , following https://code.google.com/p/java-libpst/

In my sample pst file, there are several mails. In one of the mail of that pst file, the attachment is also a mail. While parsing that PSTMessage, it's not able to fetch even, attachment's name. Please find the sample code.

 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();

Though the program is giving the exact count of the mail's attachments. But it's not able to provide, the attached mail's name or extracting it's content. Can anyone suggest a way what should I do?

Mykola
  • 3,343
  • 6
  • 23
  • 39
buddy86
  • 1,434
  • 12
  • 20

3 Answers3

4

Finally, I'm able to read the mail which is an attachment of a mail. There is a method getEmbeddedPSTMessage() in PSTAttachment class. First I need to check whether it's a normal attachment or a mail. For that we need to refer getAttachMethod(). If it returns 5 it's an embedded message. For details, please check out the documentation of PSTAttachment.

if (attach.getAttachMethod() == 5) {
     PSTMessage attachEmail = attach.getEmbeddedPSTMessage();
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
buddy86
  • 1,434
  • 12
  • 20
3

Have you tried to convert the EmbeddedPSTMessage into actual .msg format?

I am using the the library to read the pst file and the second process is I am converting the PSTMessage into javax.mail.internet.MimeMessage and save it as .eml.

The problem I have now is that whenever the attachment is embedded message (.msg format), it is getting converted to .dat extension.

Would you know how to convert the EmbeddedPSTMessage and attach it as the original msg format?

Im really desperate right now.

Below are the codes snippet:

// 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;
Porkee
  • 31
  • 4
0

JPST exports embedded messages as attachments in the original .msg file format. As I remember they have method in the Attachment object to identify if attachment is embedded message.

JPST is another library, not free one you use, but worth to try.

Example:

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());
    }
}
Marvin
  • 13,325
  • 3
  • 51
  • 57
Sneq
  • 1
  • Can you please explain with an example? – Phani Jan 13 '16 at 19:40
  • Yes. You're right @Sneq. But I wanted something which is free. – buddy86 Jan 14 '16 at 16:46
  • Example 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()); } } – Sneq Jan 14 '16 at 20:39