javax.mail.Message尝试获取给定日期范围的消息

在我的应用程序中,我正在尝试使用java邮件API来读取我们收到退回电子邮件记录的一个邮箱,我相信我们可以使用它来获取所有邮件

// Get a Store object that implements the specified protocol. store = session.getStore(protocol); //Connect to the current host using the specified username and password. store.connect(hostName, userName, password); folder = store.getFolder(folderName); Message[] messages = folder.getMessages(); 

然而,这将返回我提供的文件夹中的所有消息,有没有办法在我可以找到我在昨天收到的给定日期范围内的消息。

在这方面的任何帮助将受到高度赞赏。

谢谢

Vaibhav的

请参阅folder.search()方法以及javax.mail.search包中的许多搜索项。

请注意,IMAP搜索是在服务器上完成的,但只能解决天数,而不是时间。 通过将所有消息下载到客户端并在那里搜索来完成POP3搜索; 可能不是你想做的。

我做了以下改变,按照我的期望做了这项工作:

 cal.add(Calendar.DAY_OF_MONTH, -1); // We would get the bounce mails received yesterday ReceivedDateTerm term = new ReceivedDateTerm(ComparisonTerm.EQ,newDate(cal.getTimeInMillis())); Message[] messages = folder.search(term) 

干杯! Vaibhav的