Java get unread emails

user1700594 picture user1700594 · Jan 16, 2014 · Viewed 11k times · Source

I'm writing a code to read email from gmail. So the first time I launch, it reads new emails. That's fine. But I want when I launch it a second time, it doesn't get same emails it got before.

For example if there are 3 unread emails, when I launch for the first time, it gets the 3. But When I launch again, it gets nothing (because it already got 3). And if there is a new email and I launch it again, it should get only the last one and not the 3 firsts.

Hope I'm clear enough.

I use the code from http://alvinalexander.com/java/javamail-search-unseen-unread-messages-mailbox-pop3

package javamailtests;

import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;

public class JavaMailSearchInbox {

  public static void main(String args[]) throws Exception {

    // mail server info
    String host = "pop.gmail.com";
    String user = "USER";
    String password = "PASS";

    // connect to my pop3 inbox in read-only mode
    Properties properties = System.getProperties();
    Session session = Session.getDefaultInstance(properties);
    Store store = session.getStore("pop3");
    store.connect(host, user, password);
    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_ONLY);

    // search for all "unseen" messages
    Flags seen = new Flags(Flags.Flag.SEEN);
    FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
    Message messages[] = inbox.search(unseenFlagTerm);

    if (messages.length == 0) System.out.println("No messages found.");

    for (int i = 0; i < messages.length; i++) {
      // stop after listing ten messages
      if (i > 10) {
        System.exit(0);
        inbox.close(true);
        store.close();
      }

      System.out.println("Message " + (i + 1));
      System.out.println("From : " + messages[i].getFrom()[0]);
      System.out.println("Subject : " + messages[i].getSubject());
      System.out.println("Sent Date : " + messages[i].getSentDate());
      System.out.println();
    }

    inbox.close(true);
    store.close();
  }
}

Answer

Davex picture Davex · May 22, 2014

I've just made this ugly example. And it worked, it retrieves all messages including those that have been seen.

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);

try {
    Store store = App.session.getStore("imaps");
    store.connect("imap-mail.outlook.com", "email", "password");

    Folder folder = App.store.getFolder("Inbox");
    folder.open(Folder.READ_ONLY);
    Message[] msgs = folder.getMessages();

    for (Message msg : msgs) {
        System.out.println(msg.getSubject());
    }
}catch(MessagingException e)    {
    System.out.println(e);
}

Of course you will have to properly retrieve just the mails you need, otherwise if you have an old email account this code will start a very heavy process.

I hope this could be helpfull.