Read Outlook MSG File using Java

The MSG is an email file format used by MS Outlook. It is an Outlook item that allows storing emails, contacts, messages, tasks, appointments, etc. In certain cases, we may need to read Outlook MSG files in Java applications and extract properties such as subject, recipients, sender, body, etc. In this article, we will learn how to read an Outlook MSG file using Java.

The following topics shall be covered in this article:

Java API to Read Outlook MSG File

For reading the Outlook MSG file, we will be using Aspose.Email for Java API. It allows creating, sending, reading, and manipulating email messages seamlessly. It also supports the parsing of Outlook MSG files and provides their content in Java objects. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.

<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

We can parse and read an Outlook MSG file by following the steps given below:

  1. Load an MSG file using the MapiMessage.load() method.
  2. Read properties such as SenderName, Subject, Body, Recipients from the loaded MSG.

The following code sample shows 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

We can also save the attachments from an Outlook MSG file by following the steps given below:

  1. Load an MSG file using the MapiMessage.load() method.
  2. Loop through the attachments collection associated with the MapiMessage object.
  3. Save each attachment to disk using the save() method.

The following code sample 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

We can read the email messages embedded within the Outlook MSG attachment as well by following the steps given below:

  1. Load an MSG file using the MapiMessage.load() method.
  2. Get the attached message as a MapiMessage object.
  3. Show message properties.

The following code sample shows how to read an embedded message as an attachment using Java.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to read the content of the Outlook MSG file programmatically using Java. Moreover, we have seen how to read and extract attachments from an MSG file. Besides, you can learn more about Aspose.Email for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also