0

这是我的代码,它应该收到未读消息。但是,它正在返回我收件箱中的最后一条消息。有什么建议么?

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import java.util.*;
import com.sun.mail.pop3.POP3SSLStore;

public class MailClient {
public static void main(String[] args) {
    try {
      Properties props = new Properties();
      props.setProperty("mail.pop3.connectionpooltimeout", "3000");
      props.setProperty("mail.pop3.connectiontimeout", "3000");
      props.setProperty("mail.pop3.timeout", "3000");

      Session session;

          URLName url = new URLName("pop3","pop.gmail.com",995,"","email@gmail.com","pwd");
    session = Session.getInstance(props, null);
    Store store = new POP3SSLStore(session,url);
    store.connect();

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    Flags seen = new Flags(Flags.Flag.SEEN);
    FlagTerm unseenFlagTerm = new FlagTerm(seen,false);
    Message message[] = folder.search(unseenFlagTerm); 

    for (int i=0, n = message.length;i<20;i++) {
        System.out.println("Message " + (i + 1));
                    System.out.println("From : " + message[i].getFrom()[0]);
                    System.out.println("Subject : " + message[i].getSubject());
                    System.out.println("Sent Date : " + message[i].getSentDate());
    }
    folder.close(false);
    store.close();
    }
    catch (MessagingException e) {
            System.out.println("Error: " + e);
        }
    }
}
4

2 回答 2

3

POP3 协议不支持任何标志,因此它无法告诉您消息是否已被阅读。有关详细信息,请参阅com.sun.mail.pop3 包的 javadocs

于 2012-10-19T06:33:54.100 回答
0

可否尝试如下使用并试一试;

int messageCount =  folder.getUnreadMessageCount();

if(messageCount>0){
 Message[] messages = folder.getMessages();
}
于 2012-10-19T05:03:39.810 回答