import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import com.sun.mail.imap.IMAPFolder;
// Define the used properties.
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps"); store.connect("imap.googlemail.com", "username@gmail.com", "password");
folder = (IMAPFolder) store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
System.out.println(messages.length);
for (int i = 0; i < messages.length; i++) {
System.out.println("*****************************************************************************");
System.out.println("MESSAGE " + (i + 1) + ":");
Message msg = messages[i];
// System.out.println(msg.getMessageNumber());
// Object String;
// System.out.println(folder.getUID(msg)
subject = msg.getSubject();
System.out.println("Subject: " + subject);
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
folder.close(true);
} catch (Exception e) {
// do nothing
}
try {
store.close();
} catch (Exception e) {
// do nothing
}
}