Read Emails in Java

In certain cases, you may need to read and parse EML or MSG email messages from within your applications. In order to do it programmatically, this article shows how to extract information from the email messages in Java. Particularly, you will learn how to read email’s subject, to, from, body, and header information.

Java API to Read Emails

In order to read the email messages, we’ll use Aspose.Email for Java. It is a powerful API that lets you create feature-rich email clients in Java. You can either download the API’s JAR or install it using the following Maven configurations.

<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>21.3</version>
    <classifier>jdk16</classifier>
</dependency>

Read an Email Message using Java

The following are the steps to read an email message in Java.

  • Load the email file using MailMessage class.
  • Read the content of the email using MailMessage object, such as, To, From, Subject, Body, etc.

The following code sample shows how to read an email message using Java.

Extract Email Body as Plain Text in Java

You can also read an email and extract its body as plain text instead of HTML. The following are the steps to perform this operation.

The following code sample shows how to extract email body as plain text in Java.

Extract Header Information from an Email in Java

Aspose.Email for Java also allows you to read header information of an email message. The following are the steps to perform this operation.

The following code sample shows how to read header of the email message using Java.

Decode Email Headers in Java

You can also decode the email headers using the following steps.

The following code sample shows how to decode an email header value in Java.

Get a Free API License

You can request a free temporary license in order to use the API without evaluation limitations.

Live Demo

Conclusion

In this article, you have learned how to read email messages using Java. Furthermore, you have seen how to extract header information from an email message. You can explore more about the Java email API using documentation. In addition, you can contact us via our forum in case of any queries.

See Also