3

I am trying to write a simple java program which returns me all the unread email from my hotmail account using javamail api. This is the code I am using :

        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = new Properties();
    props.setProperty("mail.pop3.ssl.enable", "true");
    props.setProperty("mail.pop3s.socketFactory.class", SSL_FACTORY); 
    props.setProperty("mail.pop3s.socketFactory.fallback", "false"); 
    props.setProperty("mail.pop3s.port", "995"); 
    props.setProperty("mail.pop3s.socketFactory.port", "995");
    Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        Store store = session.getStore("pop3");
        store.connect("pop3.live.com", username, password);
        System.out.println(store);

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        Message messages[] = inbox.search(ft);

What is my mistake in this code? Because I am getting all the mails instead of just the unread ones.

yashdosi
  • 1,186
  • 1
  • 18
  • 40

1 Answers1

2

Quoting from Sun's documentation about their bundled POP3 provider (which I'm assuming you are using) - the documentation is located in /docs/sundocs

POP3 supports no permanent flags (see Folder.getPermanentFlags()). In particular, the Flags.Flag.RECENT flag will never be set for POP3 messages. It's up to the application to determine which messages in a POP3 mailbox are "new". There are several strategies to accomplish this, depending on the needs of the application and the environment: A simple approach would be to keep track of the newest message seen by the application. An alternative would be to keep track of the UIDs (see below) of all messages that have been seen. Another approach is to download all messages into a local mailbox, so that all messages in the POP3 mailbox are, by definition, new. All approaches will require some permanent storage associated with the client.

I think that pretty much answers your question

JAVAGeek
  • 2,674
  • 8
  • 32
  • 52