In this short tutorial, we'll show how to read an email in Java and also how to get email sender data in Java using javax.mail library. In the code sample below, we'll be reading Gmail, count received unread messages, and print sender data on the console.


Let's first add javax.mail Maven dependency that will help us with reading our mail:


Let's see how to check the contents of Inbox and read sender data from email:

package com.javahowtos.javaemail.demo;

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class JavaEmailExample {
    public static void main(String[] args) {
        getEmailMessages("", "", "yourpass");

    public static void getEmailMessages(String host, String username, String password) {
        try {
            // create properties
            Properties properties = new Properties();

            properties.put("", host);
            properties.put("mail.imap.port", "993");
            properties.put("mail.imap.starttls.enable", "true");
            properties.put("", host);

            Session emailSession = Session.getDefaultInstance(properties);

            // create the imap store object and connect to the imap server
            Store store = emailSession.getStore("imaps");
            store.connect(host, username, password);
            // create the inbox object and open it
            Folder inbox = store.getFolder("Inbox");

            // retrieve the messages from the folder in an array and print it
            Message[] messages = FlagTerm(new Flags(Flag.SEEN), false));
            System.out.println("messages.length---" + messages.length);
            for (Message message : messages) {
                InternetAddress sender = (InternetAddress) message.getFrom()[0];
                System.out.println("Sender name : " + sender.getPersonal());
                System.out.println("Sender email : " + sender.getAddress());
        } catch (Exception e) {

Since it's more useful to extract email and sender name separately from the Address object, cast Address array to InternetAddress array.

In case you're getting a message like this while executing this code, please check your firewall and antivirus since they are probably blocking the request:

javax.mail.MessagingException: PKIX path building failed: unable to find valid certification path to requested target;
  nested exception is: PKIX path building failed: unable to find valid certification path to requested target
	at com.sun.mail.imap.IMAPStore.protocolConnect(
	at javax.mail.Service.connect(
	at javax.mail.Service.connect(
	at filemanagement.Class2.getEmailMessages(
	at filemanagement.Class2.main(
Caused by: PKIX path building failed: unable to find valid certification path to requested target

In my case Avast was blocking the request towards Gmail, so everything worked fine after pausing it.