JavaMail使用IMAP阅读最近未读的邮件

时间:2015-02-24 05:58:42

标签: java javamail imap

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

4 个答案:

答案 0 :(得分:23)

这是一个例子。不要忘记在类路径中添加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() );
    }
  }
}

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

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

  1. 替换        inbox.open( Folder.READ_ONLY );        通过        inbox.open( Folder.READ_WRITE );
  2. 然后在阅读完消息后,设置如下标志:

    message.setFlag(Flags.Flag.SEEN, true);
    
  3. 完整示例:

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

答案 3 :(得分:0)

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

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