Abhendra Singh
Abhendra Singh

Reputation: 1957

Java Mail Api not returning all mails

Hi I am trying to get all mails of Gmail Inbox but it is not returning all mails. It returns few mails which are too old. Not latest. I am testing with below code:

public class ReadEmail {

    public static void check(String host, String storeType, final String user, final String password) {
        try {

            // create properties field
            Properties properties = new Properties();

            properties.put("mail.pop3.host", host);
            properties.put("mail.pop3.port", "995");
            properties.put("mail.pop3.starttls.enable", "true");
            Session emailSession = Session.getDefaultInstance(properties);
            emailSession.setDebug(true);

            // create the POP3 store object and connect with the pop server
            Store store = emailSession.getStore("pop3s");

            store.connect(host, user, password);

            // create the folder object and open it
            Folder emailFolder = store.getFolder("Inbox");
            emailFolder.open(Folder.READ_ONLY);

            // retrieve the messages from the folder in an array and print it
            Message[] messages = emailFolder.getMessages();
            System.out.println("messages.length---" + messages.length);

            // close the store and folder objects
            emailFolder.close(false);
            store.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        String host = "pop.gmail.com";
        String mailStoreType = "pop3";
        String username = "****@gmail.com";// change accordingly
        String password = "****";// change accordingly

        check(host, mailStoreType, username, password);

    }
}
  1. I am not getting what is wrong in my code. I have done other settings as given here.

  2. I also want to get only Primary tab mails. How to apply tab level filter in my code?

Upvotes: 2

Views: 640

Answers (2)

Gamby
Gamby

Reputation: 592

Taken from here

Q: Why don't I see all my messages when accessing Gmail with POP3?

A: Gmail has settings that control which of your messages are available via the POP3 protocol. See the Gmail settings page to change the configuration of your Gmail account.

Regarding POP3:

POP3 is a very limited protocol for accessing a single mailbox. It is much less capable than IMAP

Upvotes: 3

Abhendra Singh
Abhendra Singh

Reputation: 1957

After using IMAP server instead of using POP server, It worked fine. I don't know the reason but It is working fine with IMAP server.

Upvotes: 1

Related Questions