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

Popular posts from this blog

Android: where to run keytool command in android

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine