jodd mail EmailFilter does not work

132 Views Asked by At

I use

receiveEmailAndMarkSeen(EmailFilter.filter().flag(Flags.Flag.SEEN,true));

to filter the message which I don't read, but the function returns empty array. If I set false it returns all the message.

I test many times, I don't know what's wrong

Could someone help me?

1

There are 1 best solutions below

1
igr On BEST ANSWER

This may depend on the target server. Are you using IMAP or POP server? IMAP is the one that usually works better with your use case.

Here is an example that worked for me:

    ImapServer imapServer = MailServer.create()
        .host("imap.gmail.com")
        .ssl(true)
        .auth("user", "password")
        .buildImapMailServer();

    ReceiveMailSession session = imapServer.createSession();
    session.open();

    ReceivedEmail[] mails =
            session.receiveEmailAndMarkSeen(
            EmailFilter.filter().flag(Flags.Flag.SEEN, false));

    Arrays.stream(mails).map(CommonEmail::subject).forEach(System.out::println);

    session.close();

My mailbox:

enter image description here

Program output:

enter image description here

p.s. try to download all emails and check the Flag of received emails. Also, use IMAP.