How to read mail from outlook pst file using java

This is program which will read all e-mail from .pst file (outlook file).
-------------------------------------------------------------------------------

package com.pack.eml;
import com.pff.*;
import java.util.*;


public class ReadMail {  
public static void main(String[] args) {
new ReadMail("D:/NewWorkSpace/outlook.pst");  
               // Enter the path of pst file from which you want to read mail....
}
  public ReadMail(String filename) {
try {
System.out.println("File to be read:"+filename);
PSTFile pstFile = new PSTFile(filename);
System.out.println(pstFile.getMessageStore().getDisplayName());
  processFolder(pstFile.getRootFolder());
} catch (Exception err) {
err.printStackTrace();
}
  }
  int depth = -1;
  public void processFolder(PSTFolder folder) throws PSTException, java.io.IOException {
depth++;
// the root folder doesn't have a display name
if (depth > 0) {
printDepth();
System.out.println("--Folder Display Name--"+folder.getDisplayName());
}


  // go through the folders...
if (folder.hasSubfolders()) {
Vector<PSTFolder> childFolders = folder.getSubFolders();
for (PSTFolder childFolder : childFolders) {
processFolder(childFolder);
}
}
        
// and now the emails for this folder
if (folder.getContentCount() > 0) {
boolean flag=true;
depth++;
PSTMessage email = (PSTMessage)folder.getNextChild();
while (email != null) {
printDepth();
System.out.println("From: "+email.getSenderEmailAddress());
System.out.println("\t-To: "+email.getDisplayTo());
System.out.println("\t-CC: "+email.getDisplayCC());
System.out.println("\t-BCC: "+email.getDisplayBCC());
System.out.println("\t-Subject: "+email.getSubject());
System.out.println("\t-Message: "+email.getBody());
                       int numberOfAttachments = email.getNumberOfAttachments();
                      if(email.hasAttachments()){
                         PSTAttachment pstAttach = email.getAttachment(0);
                String file =pstAttach.getLongFilename();
                pstAttach.getAttachmentContentDisposition();              
                 String file1=pstAttach.getPathname();
               System.out.println("Attacment and File name"+numberOfAttachments+""+file+"PathName"+file1);
                        }
                      System.out.println("==========================================");
                     email = (PSTMessage)folder.getNextChild();
  }
depth--;
}else{
System.out.println("===No email==");
}
depth--;
}


public void printDepth() {
for (int x = 0; x < depth-1; x++) {
System.out.print(" | ");
}
System.out.print(" |- ");
}
}


-------------------------------------------------------------------------------------------

Download required jar file from the site: http://code.google.com/p/java-libpst/downloads/list

Comments

  1. Do any one know about RFC822 format for e-mail message? How to make a email in RFC822 format????

    ReplyDelete
  2. Check out JPST http://www.independentsoft.de

    With this API is possible to export/read items as .msg and .eml (RFC822) files.

    ReplyDelete
  3. Very informative, Thanks for sharing, But I came across another java email component by the name of Aspose.Email for Java. It enables Java applications to read and write MS Outlook MSG files from within a Java application adn also support Exchange server as well. I am sure it will be helpful.

    ReplyDelete
    Replies
    1. Aspose.Email for Java has limit read 50 email in subfolder, you need buy license :(

      Delete
  4. This post explains how to read emails from an outlook pst file using java and import eml to outlook 2013. Convert emails from pst files (including attachments) and export them as eml file. Outlook has an extensive history which can be accessed via Microsoft Outlook PST file. One way to access PST file is by using Java library PST library for creating read access to outlook pst file. Importing your outlook pst file into outlook 2013 is simple with these easy instructions.

    ReplyDelete

Post a Comment

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

How to create mail message in FRC822 format