我知道如何使用关键字搜索消息,我也知道如何搜索未读消息。但我不知道如何将这两件事结合起来。这是我的代码:
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();
}