Creating and Reading Outlook Files with Apache POI

Apache POI is a powerful Java library that enables you to work with Microsoft Office documents, including Outlook files. In this tutorial, we will focus on creating and reading Outlook files using Apache POI.

Example Code

Before we delve into the details, let's take a look at a simple example of how to create and read an Outlook file using Apache POI:

import org.apache.poi.hsmf.*;
import org.apache.poi.hsmf.datatypes.*;
import org.apache.poi.hsmf.exceptions.*;
import org.apache.poi.poifs.filesystem.*;

public class OutlookFilesExample {
  public static void main(String[] args) throws Exception {
    MAPIMessage msg = new MAPIMessage();
    msg.setSubject("Hello, world!");
    msg.setMessageDate(new MAPIProperty(MAPIProperty.PR_MESSAGE_DELIVERY_TIME, new Date()));
    msg.setConversationTopic("Test Email");
    msg.setSender(new MAPIProperty(MAPIProperty.PR_SENDER_NAME, "John Doe"));
    msg.setRecipient(RecipientType.TO, new MAPIProperty(MAPIProperty.PR_DISPLAY_NAME, "Jane Smith"));
    PropertySetFactory psf = new PropertySetFactory();
    try (POIFSFileSystem fs = new POIFSFileSystem()) {
      fs.writeFilesystem(new File("output.msg"));
    try (POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("output.msg"))) {
      MAPIMessage readMsg = new MAPIMessage(fs);
      String subject = readMsg.getSubject();
      String sender = readMsg.getSender().toString();
      // Print the subject and sender of the read Outlook file
      System.out.println("Subject: " + subject);
      System.out.println("Sender: " + sender);

Step-by-Step Tutorial

  1. Create an instance of the MAPIMessage class, which represents an Outlook file.
  2. Set the properties of the Outlook file, such as subject, message class, date, conversation topic, sender, and recipient.
  3. Create a PropertySetFactory instance and set the property stream of the Outlook file using the createNew() method.
  4. Write the Outlook file to a file system using the POIFSFileSystem and writeFilesystem() methods.
  5. To read an Outlook file, open it using the POIFSFileSystem and create a MAPIMessage instance.
  6. Access the desired properties of the Outlook file, such as subject and sender.

Common Mistakes

  • Not setting the required properties for the Outlook file, such as subject, sender, and recipient.
  • Using incorrect property types or values, leading to compatibility issues.
  • Missing the necessary dependencies in the project's build configuration.
  • Not properly handling exceptions when working with Outlook files.

Frequently Asked Questions (FAQs)

  1. Can I add attachments to an Outlook file using Apache POI?

    Yes, you can add attachments to an Outlook file by using the addAttachment() method provided by the MAPIMessage class.

  2. Can I read the content of an Outlook email body using Apache POI?

    Yes, you can retrieve the email body content by accessing the appropriate properties of the MAPIMessage object, such as getTextBody() or getHtmlBody().

  3. Is it possible to modify the properties of an existing Outlook file using Apache POI?

    Yes, you can open an existing Outlook file using the POIFSFileSystem, modify the properties of the MAPIMessage object, and save it back to the file system.

  4. Can Apache POI handle Outlook files from different versions of Microsoft Outlook?

    Yes, Apache POI supports Outlook files from various versions of Microsoft Outlook, including older versions.

  5. Is it possible to extract attachments from an Outlook file using Apache POI?

    Yes, you can extract attachments by iterating over the attachment properties of the MAPIMessage object and saving the attachment data to a file.


In this tutorial, we have explored how to create and read Outlook files using Apache POI. We provided example code, explained the steps involved, highlighted common mistakes, and answered frequently asked questions. With this knowledge, you can now programmatically create and manipulate Outlook files using Apache POI, enabling you to integrate Outlook functionality into your Java applications.