java imap从某个日期获取消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/870045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java imap fetch messages since a date
提问by Chaitan
I am writing/learning to fetch email using java from an IMAP folder using javax.mail package. I was successfully able to retrieve the last n messages in a Folder, however I am looking to build an example to retrieve messages since a specified date. Any examples?
我正在编写/学习使用 javax.mail 包从 IMAP 文件夹中使用 java 获取电子邮件。我成功地检索了文件夹中的最后 n 条消息,但是我正在寻找构建一个示例来检索自指定日期以来的消息。有什么例子吗?
采纳答案by diciu
You could also use the SearchTerm classes in the java mail package.
您还可以使用 java 邮件包中的 SearchTerm 类。
SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LT, someFutureDate);
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, somePastDate);
SearchTerm andTerm = new AndTerm(olderThan, newerThan);
inbox.search(andTerm);
Some combination of the above should prove to be a better way to get dates within a certain range.
以上的一些组合应该被证明是在一定范围内获取日期的更好方法。
回答by Chaitan
Here is what I came up with. This works for me, but probably not the best way to go about it. Any suggestions to improve this?
这是我想出的。这对我有用,但可能不是最好的方法。有什么建议可以改进吗?
Date from; //assume initialized
Store store; //assume initialized
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int end = inbox.getMessageCount();
long lFrom = from.getTime();
Date rDate;
long lrDate;
int start = end;
do {
start = start - 10;
Message testMsg = inbox.getMessage(start);
rDate = testMsg.getReceivedDate();
lrDate = rDate.getTime();
} while (lrDate > lFrom);
Message msg[] = inbox.getMessages(start, end);
for (int i=0, n=msg.length; i<n; i++) {
lrDate = msg[i].getReceivedDate().getTime();
if (lrDate > lFrom) {
System.out.println(i + ": "
+ msg[i].getFrom()[0]
+ "\t" + msg[i].getSubject());
}
}
回答by diciu
Instead of fetching all messages you should try taking advantage of server side search. This works by using the searchmethod of javax.mail.Folder. You will probably have to write your own SearchTerm based on a criteria on Message.getReceivedDate().
您应该尝试利用服务器端搜索,而不是获取所有消息。这通过使用javax.mail.Folder的搜索方法起作用。您可能必须根据 Message.getReceivedDate() 上的条件编写自己的 SearchTerm。
If server side search does not work, you could try using a fetch profile, i.e. instead of inbox.getMessages() use inbox.fetch(Message[] msgs, FetchProfile fp). The javadoc for fetch says: Clients use this method to indicate that the specified items are needed en-masse for the given message range. Implementations are expected to retrieve these items for the given message range in a efficient manner. Note that this method is just a hint to the implementation to prefetch the desired items.
如果服务器端搜索不起作用,您可以尝试使用提取配置文件,即代替 inbox.getMessages() 使用 inbox.fetch(Message[] msgs, FetchProfile fp)。fetch 的 javadoc 说:客户端使用此方法来指示对于给定的消息范围需要整体指定的项目。期望实现以有效的方式检索给定消息范围的这些项目。请注意,此方法只是对预取所需项目的实现的提示。
回答by Javed Ahmed
public class CheckDate {
public void myCheckDate(Date givenDate) {
SearchTerm st = new ReceivedDateTerm(ComparisonTerm.EQ,givenDate);
Message[] messages = inbox.search(st);
}
// in main method
public static void main(String[] args) throws ParseException{
SimpleDateFormat df1 = new SimpleDateFormat( "MM/dd/yy" );
String dt="06/23/10";
java.util.Date dDate = df1.parse(dt);
cd.myCheckDate(dDate);
}
}
回答by Ray Hulha
All mails in the last month:
上个月的所有邮件:
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.MONTH, false);
Message[] search = folder.search(new ReceivedDateTerm(ComparisonTerm.GT, cal.getTime()));