PST (Personal Storage Table) is a well-known file format that is used for storage purposes by different Microsoft programs such as MS Outlook, Exchange, and Windows Messaging. PST files can store messages, contacts, and information about other items such as calendars, events, etc. In certain cases, you may need to parse a PST file and extract data from it programmatically. To achieve that, this article shows how to read MS Outlook PST files using Java. With the help of code samples, you will learn how to extract folders’ information, read emails and fetch contacts from a PST file.
- Java API to Read PST Files
- Read an Outlook PST File
- Read Emails from a PST File
- Extract Contacts from a PST File
Java API to Read Outlook PST Files
Aspose.Email for Java is a popular and feature-rich email processing API for creating and sending emails. In addition, it allows you to work with popular email clients and storage file formats. In this article, we will use Aspose.Email for Java to read messages and other information from PST files. You can either download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-email</artifactId>
<version>22.3</version>
<classifier>jdk16</classifier>
</dependency>
Read an Outlook PST File in Java
The following are the steps to read a PST file and extract its information.
- Load the PST file using PersonalStorage.fromFile() method.
- Get the folders collection in PST using PersonalStorage.getRootFolders().getSubFolders() method.
- Retrieve the information of the folders such as name, number of items, etc.
The following code sample shows how to read a PST file and fetch folders’ information.
Read Emails from a PST File in Java
The following are the steps to read email messages from a PST file in Java.
- Load the PST file using PersonalStorage.fromFile() method.
- Access the root folder using PersonalStorage.getRootFolder() method.
- Get message collection from each subfolder using FolderInfo.getContents() method.
- Loop through the messages in the collection and read each message’s fields.
The following code sample shows how to extract messages from a PST file in Java.
Extract Contacts from a PST File in Java
In certain cases, you may need to extract the contacts’ information stored in a PST file. The following steps show how to access the contacts in a PST file in Java.
- Load the PST file using PersonalStorage.fromFile() method.
- Get reference of the contacts folder using PersonalStorage.getRootFolder().getSubFolder(“Contacts”) method.
- Get collection of contacts using FolderInfo.getContents() method.
- Loop through the contacts collection and read each contact or save it.
The following code sample shows how to extract contacts from a PST file in Java.
Get a Free API License
You can try Aspose.Email for Java for free by getting a temporary license.
Conclusion
In this article, you have learned how to read PST files programmatically in Java. Furthermore, you have seen how to extract folder information, fetch emails, and retrieve contacts from a PST file in Java. In addition, you can explore more about Aspose.Email for Java using documentation. Also, in case you would have any questions or queries, feel free to let us know via our forum.