简体   繁体   English

仅从 Gmail 获取看不见的邮件 - imap

[英]Get only unseen mails from Gmail - imap

I have this code:我有这个代码:

Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imaps");
    
try(Store store = Session.getDefaultInstance(properties, null).getStore("imaps")){

    store.connect("imap.gmail.com", 993, username, password);

    Folder inboxFolder = store.getFolder("INBOX");
    
    try(inboxFolder){
    
        //open folder
        inboxFolder.open(Folder.READ_WRITE);
        
        //get messages
        Message[] messages = inboxFolder.search(new FlagTerm(new Flags(Flags.Flag.RECENT), true));
        
        Arrays.stream(messages)
            .forEach(message -> {
            
                try{
                
                    System.out.println(message.getSubject());
                    
                }catch(Exception e){ e.printStackTrace(); }
                
            };  
                
    }

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

I try to read all unseen mails from INBOX from GMail.我尝试从 GMail 的收件箱中读取所有看不见的邮件。

But in console is printed nothing and messages.length is 0.但在控制台中没有打印任何内容, messages.length为 0。

How I can read just unseen mails?我如何阅读看不见的邮件?

PS: I just printed the flag of all messages(doesn't matter the flag) and I realized the unseen messages has no flag. PS:我刚刚打印了所有消息的标志(与标志无关),我意识到看不见的消息没有标志。 This is how it is supposed to be?这就是它应该的样子?

PS: I found out. PS:我发现了。

Message[] messages = inboxFolder.search(
    new FlagTerm(new Flags(Flags.Flag.SEEN), false)
);

With this one works: new FlagTerm(new Flags(Flags.Flag.SEEN), false)有了这个作品: new FlagTerm(new Flags(Flags.Flag.SEEN), false)

I found the solution.我找到了解决方案。

Instead of代替

new FlagTerm(new Flags(Flags.Flag.RECENT), true)

I need to do this我需要这样做

new FlagTerm(new Flags(Flags.Flag.SEEN), false)

To get the unseen messages.获取看不见的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM