我有一个多线程应用程序,可以同时读取多个IMAP邮箱。每个线程都会打开已配置的文件夹,例如INBOX,读取所有未读邮件,然后关闭/清除文件夹。
private void readMail(ImapMailbox mailbox) throws Exception {
Properties props = new Properties();
props.put("mail.debug", mailbox.isDebug());
props.put("mail.imap.connectiontimeout", connectionTimeout);
props.put("mail.imap.timeout", timeout);
Session session = Session.getDefaultInstance(props);
Store store = session.getStore(new URLName(imapUrl(mailbox)));
Folder folder = null;
try {
store.connect();
folder = store.getFolder(mailbox.getFolder());
folder.open(Folder.READ_WRITE);
for (Message message : folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false))) {
if (message == null) {
logger.warn("Mail message is null for mailbox {}!!", mailbox.getEmail());
continue;
}
try {
sender.sendEmailToQueue(mailbox, message);
message.setFlag(Flags.Flag.DELETED, true);
} catch (Throwable ex) {
logger.error("Failed to add email to queue", ex);
}
}
} finally {
if (folder != null && folder.isOpen()) {
try {
folder.close(true);
} catch (Exception ex) {
logger.warn("Problem closing folder {}", folder.getFullName(), ex);
}
}
if (store != null && store.isConnected()) {
try {
store.close();
} catch (Exception ex) {
logger.warn("Problem closing store", ex);
}
}
}
}
如果将一个邮箱配置为从INBOX读取,而另一个邮箱配置为从INBOX的子文件夹读取,例如INBOX / customFolder,则父文件夹关闭。这还会关闭子文件夹吗?
还有谁能解释为什么我在循环中收到空消息?有时我会在日志中看到此信息: logger.warn(“邮箱{} !!的邮件为空,”,Mailbox.getEmail());