在测试 javax.mail 中的 imap 协议 idle 时会有olderClosedException异常。
原因:
在Messaging对象上调用方法并且拥有该对象的Folder由于某种原因而死亡时,将引发此异常。
由于我没注意看文档,IMAPFolder.idle();是阻塞的而又没有循环读,导致直接关闭了资源~
收件箱中共633封邮件!
收件箱中共78封未读邮件!
收件箱中共0封新邮件!
收件箱中共0封已删除邮件!
------------------------开始解析邮件----------------------------------
支持idle
Got 1 new messages
---------------------------------------------------
-----关闭资源-----
标题: Re:测试邮件推送
javax.mail.FolderClosedException
at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:169)
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1546)
at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:808)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1508)
at io.qyi.qqBotManage.mail.mail$1.messagesAdded(mail.java:88)
at javax.mail.event.MessageCountEvent.dispatch(MessageCountEvent.java:154)
at javax.mail.EventQueue.run(EventQueue.java:170)
at java.lang.Thread.run(Thread.java:745)
import com.sun.mail.imap.IMAPFolder;
import javax.mail.*;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
import java.io.IOException;
import java.util.Properties;
/**
* @author LuoYe
* @version 1.0
* @description: TODO
* @date 2021/5/12 3:13
*/
public class mail {
public static void main(String[] args) throws MessagingException, IOException {
init();
}
public static void init() throws MessagingException, IOException {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", "outlook.office365.com");
props.setProperty("mail.imap.port", "993");
/*tls与https需要增加以下代码*/
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.socketFactory.port", "993");
Session session = Session.getInstance(props);
// 创建IMAP协议的Store对象
Store store = session.getStore("imap");
// 连接邮件服务器
store.connect("账户", "密码");
// 获得收件箱
Folder folder = store.getFolder("INBOX");
// 以读写模式打开收件箱
folder.open(Folder.READ_WRITE);
// 获得收件箱的邮件列表
Message[] messages = folder.getMessages();
// 打印不同状态的邮件数量
System.out.println("收件箱中共" + messages.length + "封邮件!");
System.out.println("收件箱中共" + folder.getUnreadMessageCount() + "封未读邮件!");
System.out.println("收件箱中共" + folder.getNewMessageCount() + "封新邮件!");
System.out.println("收件箱中共" + folder.getDeletedMessageCount() + "封已删除邮件!");
System.out.println("------------------------开始解析邮件----------------------------------");
// 解析邮件
/*for (Message message : messages) {
IMAPMessage msg = (IMAPMessage) message;
String subject = MimeUtility.decodeText(msg.getSubject());
System.out.println("[" + subject + "]未读,是否需要阅读此邮件(y/n)?");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String answer = reader.readLine();
if ("y".equalsIgnoreCase(answer)) {
System.out.println(msg.getContent().toString());
// POP3ReceiveMailTest.parseMessage(msg); // 解析邮件
// 第二个参数如果设置为true,则将修改反馈给服务器。false则不反馈给服务器
// msg.setFlag(Flag.SEEN, true); //设置已读标志
}
}*/
folder.addMessageCountListener(new MessageCountAdapter() {
@Override
public void messagesAdded(MessageCountEvent e) {
// super.messagesAdded(e);
Message[] msgs = e.getMessages();
System.out.println("Got " + msgs.length + " new messages");
for (Message msg : msgs) {
System.out.println("---------------------------------------------------");
try {
System.out.println("标题: " + msg.getSubject());
System.out.println("内容: " + msg.getContent().toString());
} catch (MessagingException messagingException) {
messagingException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
@Override
public void messagesRemoved(MessageCountEvent e) {
super.messagesRemoved(e);
}
});
if (folder instanceof IMAPFolder) {
System.out.println("支持idle");
IMAPFolder f = (IMAPFolder) folder;
f.idle();
}
System.out.println("-----关闭资源-----");
// 关闭资源
folder.close(false);
store.close();
}
}