Read Outlook MSG File using Java

The MSG format is used by Microsoft Outlook to store emails, contacts, tasks, and appointments. In Java applications you may need to read MSG files and extract details such as subject, recipients, sender, body, and attachments. This article shows how to read an Outlook MSG file using Java.

The following topics are covered:

Java API to Read Outlook MSG File

To read Outlook MSG files we use the Aspose.Email for Java API. It supports creating, sending, reading, and manipulating email messages, and it can parse MSG files into Java objects. Download the JAR from the link below or add the Maven dependency to your project.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>22.2</version>
    <classifier>jdk16</classifier>
</dependency>

Parse and Read an Outlook MSG File using Java

Follow these steps to parse and read an MSG file:

  1. Load the MSG file with the MapiMessage.load() method.
  2. Access properties such as SenderName, Subject, Body, and Recipients.

The code sample below demonstrates how to read an Outlook MSG file using Java.

Parse and Read an Outlook MSG File using Java.

Parse and Read an Outlook MSG File using Java.

Get Attachments from MSG File using Java

To save attachments from an MSG file:

  1. Load the MSG file with the MapiMessage.load() method.
  2. Iterate through the attachments collection of the MapiMessage object.
  3. Save each attachment using the save() method.

The following code shows how to save attachments from an Outlook MSG file using Java.

Get Attachments from MSG File using Java.

Get Attachments from MSG File using Java.

Read Embedded Message from Attachment

To read an email embedded in an MSG attachment:

  1. Load the MSG file with the MapiMessage.load() method.
  2. Retrieve the attached message as a MapiMessage object.
  3. Display its properties.

The code sample below illustrates how to read an embedded message as an attachment using Java.

Get a Free License

Request a free temporary license to use the API without evaluation limits.

Conclusion

This article demonstrated how to read Outlook MSG files programmatically with Java, extract message details, and save attachments. For more information, consult the Aspose.Email for Java documentation or ask questions on the forum.

See Also