Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting PST file containing mail as an attachment of PSTMessage

Tags:

java

pst

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?

like image 343
buddy86 Avatar asked Jan 11 '16 14:01

buddy86


People also ask

Does .pst file contain email attachments?

pst files include attachments or does Outlook store them somewhere else? Yes, Outlook stores all attachments to Outlook items--not just messages, but also Contacts, Tasks, and other items--as part of the item in that . pst file. This process has its pluses and minuses.


2 Answers

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();
}
like image 52
buddy86 Avatar answered Nov 14 '22 14:11

buddy86


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;
like image 22
Porkee Avatar answered Nov 14 '22 14:11

Porkee