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

JavaMail reading recent unread mails using IMAP

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

这里是例子。不要忘记在类路径中添加 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, "username@gmail.com", "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() );
    }
  }
}

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

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

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

  1. 替换 inbox.open( Folder.READ_ONLY ); 经过 inbox.open( Folder.READ_WRITE );
  2. 然后在阅读消息后,像这样设置标志:

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

完整示例:

    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, "username@gmail.com", "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);
        }
      }
    }

我认为这可能有助于访问 read/unread/recent 邮件 根据需要更改变量。

// 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);