我有一个线程每 10 秒循环检查一次我的收件箱。我在循环之前创建了我的邮件会话并存储并连接到存储:
store.connect()
while (running) {
Thread.sleep(emailReceiveInterval);
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message[] emails = inbox.getMessages();
[...]
inbox.close(true);
}
问题在于线程运行时没有收到新电子邮件。
因此,我尝试在循环中连接并关闭商店:
while (running) {
try {
Thread.sleep(emailReceiveInterval);
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message[] emails = inbox.getMessages();
[...]
finally {
inbox.close(true); // Exception handling not posted
store.close(); // Exception handling not posted
}
}
第一个循环很好,所有电子邮件都正确接收,但现在我在第二个store.connect()收到 NullPointerException :
java.lang.NullPointerException: null
at org.apache.geronimo.javamail.store.pop3.POP3Store.protocolConnect(POP3Store.java:164) ~[geronimo-javamail_1.4_mail-1.8.2.jar:1.8.2]
at javax.mail.Service.connect(Service.java:251) ~[geronimo-javamail_1.4_mail-1.8.2.jar:1.8.2]
at javax.mail.Service.connect(Service.java:91) ~[geronimo-javamail_1.4_mail-1.8.2.jar:1.8.2]
at javax.mail.Service.connect(Service.java:76) ~[geronimo-javamail_1.4_mail-1.8.2.jar:1.8.2]
at com.company.MyClass$ReceivingThread.run(MyClass.java:161) ~[target-eclipse/:na]
我该怎么做才能在每个循环中刷新收件箱?