JavaMail: MailBox always downloads the same emails

Question:

I'm downloading emails from Gmail , and storing in the database the ones that are important to my client's system.

But the problem is when I use the code below, it always returns the same emails to me, getting duplicated.

Has anyone ever experienced this?

Code:

try {
    System.out.println("asdasdasd");
    // 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);

    // 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);

    Message[] messages = emailFolder.getMessages();
    System.out.println("messages.length---" + messages.length);

    for (int i = 0, n = messages.length; i < n; i++) {
        Message message = messages[i];
        System.out.println("---------------------------------");
        System.out.println("Email Number " + (i + 1));
        System.out.println("Subject: " + message.getSubject());
        System.out.println("From: " + message.getFrom()[0]);
        System.out.println("Text: " + message.getContent().toString());
        System.out.println(message.getLineCount());

    }

    // 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();
}

Answer:

When you run Message[] messages = emailFolder.getMessages(); it returns ALL messages from the folder, which in your case is the INBOX, that's why it always brings the same messages.

You can solve the problem by applying a filter (Unread for example) in the rescue of messages, deleting messages from the folder or moving to another folder, for example:

Applying a filter when retrieving messages:
Change Message[] messages = emailFolder.getMessages(); per:

// O filtro aqui são as mensagens não visualizadas ainda
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = inbox.search(ft);

Deleting messages from folder

Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);

Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);

for (int i = 0, n = messages.length; i < n; i++) {
    Message message = messages[i];
    System.out.println("---------------------------------");
    System.out.println("Email Number " + (i + 1));
    System.out.println("Subject: " + message.getSubject());
    System.out.println("From: " + message.getFrom()[0]);
    System.out.println("Text: " + message.getContent().toString());
    System.out.println(message.getLineCount());
}

// close the store and folder objects
emailFolder.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
emailFolder.close(true);
store.close();

Moving to another folder:

Folder emailFolder = store.getFolder("INBOX");
Folder folderDestino = store.getFolder("PASTA DE DESTINO");
emailFolder.open(Folder.READ_WRITE);
folderDestino.open(Folder.READ_WRITE);

Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);

for (int i = 0, n = messages.length; i < n; i++) {
    Message message = messages[i];
    System.out.println("---------------------------------");
    System.out.println("Email Number " + (i + 1));
    System.out.println("Subject: " + message.getSubject());
    System.out.println("From: " + message.getFrom()[0]);
    System.out.println("Text: " + message.getContent().toString());
    System.out.println(message.getLineCount());
}

// close the store and folder objects
emailFolder.copyMessages(messages, folderDestino);
emailFolder.setFlags(messages, new Flags(Flags.Flag.DELETED), true);

emailFolder.close(true);
folderDestino.close(true);
store.close();

There is still the possibility for you to determine the beginning and the end of the messages that will be captured in the getMessages method, but I believe this solution is very bad.

Scroll to Top