下面是用于连接 IMAP 文件夹并对其执行操作的代码。所以我的问题是关于javax.mail.Session
在这种情况下会每秒重新创建哪个(取决于 checkInbox() 的睡眠时间和运行时)。
我确信这不是一个好的解决方案,尤其是在 IMAP 上进行轮询有点愚蠢,但我无法IMAP listener
运行。
不是每次运行都重新创建会话可能是一个更好的解决方案,但我怎么知道何时session is closed
或可以故意关闭它?但是有没有什么像Session.close()
或者是Session比NULL?或者会话上是否有一些定义的超时......
来源:
final String port = "993";
Properties prop = new Properties();
// I assume there is some redundancy here but this didn't cause any problems so far
prop.setProperty("mail.imaps.starttls.enable", "true");
prop.setProperty("mail.imaps.port", port);
/** This part can be removed
* prop.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
* prop.setProperty("mail.imaps.socketFactory.port", port);
* prop.setProperty("mail.imaps.socketFactory.fallback", "false");
*/
prop.setProperty("mail.imap.ssl.enable", "true");
prop.setProperty("mail.debug", "false");
// Create a session before you loop since the configuration doesn't change
Session session = Session.getInstance(prop);
// Nearly loop forever in Prod
while(true){
// Check the INBOX and do some other stuff
Store store = session.getStore("imaps");
store.connect(host, user, pw);
// ... the operations on the session ...
store.close();
// Sleep a bit try & catch removed
Thread.sleep(1000);
}