Quick Example of Searching Your Outlook PST File with Java

https://github.com/rjohnsondev/java-libpst

import com.pff.*;
import java.util.*;
public class Test {
 
 private java.sql.Date yesterday;
 
 public static void main(String[] args)
 {
Test c = new Test("C:UsersUSERIDDocumentsOutlook FilesMYEMAIL.pst");
 System.out.println("Mail Count:" + c.counter);
 System.out.println("Folder Count: " + c.foldercounter );
 }
// mail counter
 public int counter = 0;
// folder counter
 public int foldercounter = 0;
/**
 * 
 * Constructor for Test.
 * @param filename
 */
 public Test(String filename) {
 try {
 // Get today as a Calendar
 Calendar today = Calendar.getInstance();
 // Subtract 1 day
 today.add(Calendar.DATE, -1);
 // Make an SQL Date out of that
 yesterday = new java.sql.Date(today.getTimeInMillis());
 
 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) {
 // System.out.println("Folder Name:" + folder.getDisplayName());
 //}
// go through the folders...
 if (folder.hasSubfolders()) {
 Vector<PSTFolder> childFolders = folder.getSubFolders();
 for (PSTFolder childFolder : childFolders) {
 processFolder(childFolder);
 }
 }
//System.out.println("Folder Count:" + folder.getContentCount());
// counter += folder.getContentCount();
// and now the emails for this folder
 if (folder.getContentCount() > 0) {
 foldercounter++;
 depth++;
 PSTMessage email = (PSTMessage)folder.getNextChild();
 
 while (email != null && email.getSubject() !=null) {
 if (email.getSubject().toLowerCase().contains("MY SEARCH TERM") ) {
 System.out.println(folder.getDisplayName() + ":Email: "+email.getSubject());
 }
// count todays
 if ( email.getCreationTime().after(yesterday) ) {
 counter++;
 }
 email = (PSTMessage)folder.getNextChild();
 }
 depth--;
 }
 depth--;
 }
}



			

Leave a Reply