Reading New Emails from Java Applications

Sometimes we only want to check new emails received, we commonly see this functionality in email client where in we open the email client in morning and we see few emails in black bold text denoting new emails received. In this articles we will learn on how we can identify new emails received in our mailbox using java.

Email Exchange Servers can be POP3 and IMAP, by default we use POP3. Every email have certain flags set by which we can identify the new emails received but these functionality is not available in POP3 servers; you will have to use IMAP servers. But there are alternatives to read new emails received in POP3 servers as well but that would required some processing and storing the email ids received at local end.
Various Flags ssed in Emails are SEEN, RECENT, DELETED, ANSWERED, DRAFT, FLAGGED, USER; but now we are going to concentrate on SEEN and ANSWERED one used for checking new emails.

FTP Upload in Java

Check Unread Unread Emails in Java:

Tagged . Bookmark the permalink.

4 Responses to Reading New Emails from Java Applications

  1. Ohw great tutorial I will give it a try.. Thanks!

  2. Peter McNeil says:

    why to you call
    Flags flags = message.getFlags();
    since you don’t use the flags variable?

    Also you open the folder read_write but don’t set any flags etc.

  3. ravi gupta says:

    thanks but someone may plz tell how to get or download only those mails on my PC which are unread?here in this code we are retrieving all messages.i don’t want that.

  4. sonia says:

    thanks for this tutorial
    i want to ask you about hostname
    if i use gmail, the host name is
    i don’t no why there is an exception when i want to run the java code

    exception at this line:
    store.connect(“”, “USERNAME”,”PASSWORD”);

    Exception in thread “main” javax.mail.MessagingException: Connection timed out: connect;
    nested exception is: Connection timed out: connect
    at com.sun.mail.imap.IMAPStore.protocolConnect(
    at javax.mail.Service.connect(
    at javax.mail.Service.connect(
    at cueuj.ReadEmail.processEmail(
    at cueuj.ReadEmail.main(
    Caused by: Connection timed out: connect
    at Method)
    at com.sun.mail.util.SocketFetcher.createSocket(
    at com.sun.mail.util.SocketFetcher.getSocket(
    at com.sun.mail.iap.Protocol.(
    at com.sun.mail.imap.protocol.IMAPProtocol.(
    at com.sun.mail.imap.IMAPStore.protocolConnect(
    … 4 more
    Java Result: 1

Leave a Reply