JavaMail 使用 IMAP 读取最近未读邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28689099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:38:13  来源:igfitidea点击:

JavaMail reading recent unread mails using IMAP

javajavamailimap

提问by Ram Bavireddi

I have a requirement to retrieve unread mails from Gmail. I am using Java Mail API. By default, this API retrieves mails from the oldest to newest. But I need to retrieve recent mails first. Is it possible? Thanks in advance.

我需要从 Gmail 检索未读邮件。我正在使用 Java 邮件 API。默认情况下,此 API 从最旧到最新检索邮件。但我需要先检索最近的邮件。是否可以?提前致谢。

采纳答案by Dmitriy Dobrotvorskiy

Here is example. Do not forget to add javax.mailin your classpath.

这是示例。不要忘记在类路径中添加javax.mail

import javax.mail.*;
import javax.mail.search.FlagTerm;
import java.util.*;

public class GmailFetch {

  public static void main( String[] args ) throws Exception {

    Session session = Session.getDefaultInstance(new Properties( ));
    Store store = session.getStore("imaps");
    store.connect("imap.googlemail.com", 993, "[email protected]", "password");
    Folder inbox = store.getFolder( "INBOX" );
    inbox.open( Folder.READ_ONLY );

    // Fetch unseen messages from inbox folder
    Message[] messages = inbox.search(
        new FlagTerm(new Flags(Flags.Flag.SEEN), false));

    // Sort messages from recent to oldest
    Arrays.sort( messages, ( m1, m2 ) -> {
      try {
        return m2.getSentDate().compareTo( m1.getSentDate() );
      } catch ( MessagingException e ) {
        throw new RuntimeException( e );
      }
    } );

    for ( Message message : messages ) {
      System.out.println( 
          "sendDate: " + message.getSentDate()
          + " subject:" + message.getSubject() );
    }
  }
}

回答by Bill Shannon

JavaMail gives you an array of Message objects. The messages are in the order received. If you want to look at the most recently received messages first, go through the array in the reverse order. If you want to look at the most recently sentmessages first, you'll need to sort the array, as described in the other answer.

JavaMail 为您提供了一组 Message 对象。消息按收到的顺序排列。如果要先查看最近收到的消息,请按相反的顺序遍历数组。如果您想先查看最近发送的消息,则需要对数组进行排序,如另一个答案中所述。

回答by sangamesh

Make sure to use the IMAP protocol, as it supports for flagging.

确保使用 IMAP 协议,因为它支持标记。

Do following changes to your code:

对您的代码进行以下更改:

  1. replace inbox.open( Folder.READ_ONLY );by inbox.open( Folder.READ_WRITE );
  2. Then after reading the message, set the flag like so:

    message.setFlag(Flags.Flag.SEEN, true);
    
  1. 替换 inbox.open( Folder.READ_ONLY );inbox.open( Folder.READ_WRITE );
  2. 然后在阅读消息后,像这样设置标志:

    message.setFlag(Flags.Flag.SEEN, true);
    

Full example:

完整示例:

    import javax.mail.*;
    import javax.mail.search.FlagTerm;
    import java.util.*;

    public class GmailFetch {

      public static void main( String[] args ) throws Exception {

        Session session = Session.getDefaultInstance(new Properties( ));
        Store store = session.getStore("imaps");
        store.connect("imap.googlemail.com", 993, "[email protected]", "password");
        Folder inbox = store.getFolder( "INBOX" );
        inbox.open( Folder.READ_WRITE );

        // Fetch unseen messages from inbox folder
        Message[] messages = inbox.search(
            new FlagTerm(new Flags(Flags.Flag.SEEN), false));

        // Sort messages from recent to oldest
        Arrays.sort( messages, ( m1, m2 ) -> {
          try {
            return m2.getSentDate().compareTo( m1.getSentDate() );
          } catch ( MessagingException e ) {
            throw new RuntimeException( e );
          }
        } );

        for ( Message message : messages ) {
          System.out.println( 
              "sendDate: " + message.getSentDate()
              + " subject:" + message.getSubject() );
              message.setFlag(Flags.Flag.SEEN, true);
        }
      }
    }

回答by Vikash Kumar

I think this might help to access read/unread/recent mails change your variables according to your needs.

我认为这可能有助于访问已读/未读/最近的邮件,根据您的需要更改您的变量。

// search for all "unseen" messages

Flags seen = new Flags(Flags.Flag.SEEN);// try changing this SEEN to RECENT 

// set it true or false for seen & unseen mail

FlagTerm unseenFlagTerm = new FlagTerm(seen, false)
Message messages[] = inbox.search(unseenFlagTerm);