gpt4 book ai didi

Javamail IMAP 检索特定日期时间内的最新电子邮件

转载 作者:行者123 更新时间:2023-12-02 11:38:12 24 4
gpt4 key购买 nike

我对 javamail 的使用感到困惑。我需要时不时地从邮件服务器下载最新的邮件,但是javamail似乎无法查询小时、分钟、秒内的特定邮件,只有一天可用?

(1) 我是否应该扫描所有返回电子邮件以在时间间隔内进行额外检查?还是我做错了什么?

(2) 我是否应该假设电子邮件按收到日期升序排列并安全地忽略电子邮件的顶部部分(假设之前已检索到)

(3) 有推荐的扫描间隔吗?如果与邮件客户端相比呢?

private void retrieveEmailsWithinDateInterval() throws ParseException, MessagingException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, 1);

//Retrieval of email setting
Date previousDate = sdf.parse("2018-02-13");
Date tmrDate = new Date(c.getTimeInMillis());
String imapHost = "***";
int port = 587;
String urlNameStr = "***";
String emailAddr = "***";
String emailPass = "***";
String inboxFolderName = "***";

Properties props = System.getProperties();

props.setProperty("mail.imap.host", imapHost);
props.setProperty("mail.imap.port", Integer.toString(port));
props.setProperty("mail.store.protocol", "imap");

Session session = Session.getDefaultInstance(props, null);
URLName urlName = new URLName(urlNameStr);
Store store = session.getStore(urlName);

store.connect(imapHost, emailAddr, emailPass);

ReceivedDateTerm minDateTerm = new ReceivedDateTerm(ComparisonTerm.GT, previousDate);
ReceivedDateTerm maxDateTerm = new ReceivedDateTerm(ComparisonTerm.LT, tmrDate);
SearchTerm searchTerm = new AndTerm(minDateTerm, maxDateTerm);

IMAPFolder inboxFolder = (IMAPFolder) store.getFolder(inboxFolderName);

if(!inboxFolder.isOpen())
inboxFolder.open(Folder.READ_ONLY);

Message[] messagesInbox = inboxFolder.search(searchTerm);

for (Message msg : messagesInbox){
//(1) Should I extra check or alternative solution?
//Seem it compare date only and ignore hour, minute and second.
if (msg.getReceivedDate().getTime() <= previousDate.getTime())
continue;
//(2) Should I scan through email messages?
//Or will all mail server sort in receivedDate, that I can safely ignore top messages or bottom messages?

processEmailWithinDatetimeInterval(msg);
}

if ((inboxFolder != null) && inboxFolder.isOpen()) {
inboxFolder.close(true);
}
if (store != null) {
store.close();
}
}

最佳答案

IMAP 协议(protocol)的限制是您只能按天搜索,而不能按时间搜索。

如果您只是想在收件箱中查找以前未见过的最新消息,您可以 keep track of the UID of the last message you've seen然后ask for messages with larger UIDs ,例如,

Message[] newMessages = ((UIDFolder)folder).getMessagesByUID(lastSeen + 1, UIDFolder.MAXUID);

关于Javamail IMAP 检索特定日期时间内的最新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48774481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com