Retrieve email according to specified date time using POP3 in Java

♀尐吖头ヾ 提交于 2019-12-06 09:39:30
frankieta

Searching for a period or in general by time is done by the server only if you use IMAP to connect. Example:

SearchTerm term = null;

Calendar cal = null;
cal = Calendar.getInstance();
Date minDate = new Date(cal.getTimeInMillis());   //get today date

cal.add(Calendar.DAY_OF_MONTH, 1);                //add 1 day
Date maxDate = new Date(cal.getTimeInMillis());   //get tomorrow date
ReceivedDateTerm minDateTerm = new ReceivedDateTerm(ComparisonTerm.GE, minDate);
ReceivedDateTerm maxDateTerm = new ReceivedDateTerm(ComparisonTerm.LE, maxDate);

term = new AndTerm(term, minDateTerm);            //concat the search terms
term = new AndTerm(term, maxDateTerm);

Message messages[] = folderInbox.search(term);    //search on the imap server

If you instead of IMAP use POP3, I guess your only choice is to filter (on the client) on the entire list of messages that you fetched from the server, iterating over it, like @user2310289 was telling you:

for (Message message : messages) {
    if (message.getSentDate().after(minDate) && message.getSentDate().before(maxDate))
       {
          //do whatever you want with your filtered by period message
       } 
}

I hope I helped you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!