I know how to search messages with keyword, I also know how to search unread messages. But I don't know how to combine those two things. Here is my code:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
//GMail connecting
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", user, password);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
//Enter term to search here
BodyTerm bodyTerm = new BodyTerm("abcd");
FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
//can only search with one term at one time
Message[] foundMessages = inbox.search(bodyTerm);
}
catch (MessagingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}