In this Article, I will show you simple java program to read micro soft files
with .pst and .ost extension.
Tools Uses :
1) eclipse version Luna 4.4.1.
2) Maven 3.3.3
3) JDK 1.6 or higher
Simple Java program to read .ost and .pst files.
Simple Steps to follow,
1) Create a maven project.
2) Add java-libpst dependency to pom.xml file
3) Write a simple program to read .pst and .ost files.
4) Demo, run and verify the out put
Dependency that need to be added to pom.xml file is,
<dependency> <groupId>com.pff</groupId> <artifactId>java-libpst</artifactId> <version>0.8.1</version> </dependency>
Complete pom.xml file will be as given,
After adding the dependency.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.devjavasource.email</groupId> <artifactId>readOstPstFiles</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>readOstPstFiles</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.pff</groupId> <artifactId>java-libpst</artifactId> <version>0.8.1</version> </dependency> </dependencies> </project>
Write a simple program to read .pst and .ost files
1) Create pstFile Object, by passing the pst file as argument.
PSTFile pstFile = new PSTFile(filename);
2) Get the root folder by calling the method getRootFolder()
pstFile.getRootFolder()
3) Get SubFolder list by calling getSubFolders() method
Vector<PSTFolder> childFolders = folder.getSubFolders();
4) Get PSTMessage by type casting the child to PSTMessage class.
PSTMessage email = (PSTMessage)folder.getNextChild();
5) There are simple methods are in PSTMessage class to get email details.
like email Subject, email body, email body in Html format, recipient list…etc.
Here is complete java source code,
App.java
package com.devjavasource.email.readOstPstFiles; import com.pff.*; import java.util.*; /** * Hello world! * */ public class App { public static void main(String[] args) { //final String finalName = "C:/Venkat_06012014/Blog/eMails/pst/nallave_ost_format.ost"; final String finalName = "C:/Venkat_06012014/Blog/eMails/pst/nalla.pst"; new App( finalName ); } public App(String filename) { try { 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.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) { depth++; PSTMessage email = (PSTMessage)folder.getNextChild(); while (email != null) { printDepth(); System.out.println("Email Subject: "+email.getSubject()); System.out.println("Email Body: "+email.getBodyHTML()); //System.out.println("Email Body: "+email.getDisplayName()); email = (PSTMessage)folder.getNextChild(); } depth--; } depth--; } public void printDepth() { for (int x = 0; x < depth-1; x++) { System.out.print(" | "); } System.out.print(" |- "); } }
Demo:
Select App.java and “Run As” -> “Java Application”
nalla |- Top of Outlook data file | |- Deleted Items | |- test | | |- Email Subject: Tagging N4 - 2.6.14.1 |- Search Root |- SPAM Search Folder 2
You can download complete project, Here
readOstPstFiles
*** Venkat – Happy learning ****