Discussions
Categories
- 5.2K All Categories
- 13 Introduce Yourself!
- 414 Community Feedback - NEW! (No Product Questions)
- 100 General Community Platform Concerns/Kudos/Feedback
- 80 Community Platform Bug Reports
- 80 How Do I Use the Community?
- 48 Where is the...? (Community Platform Locations)
- 15 Ideas and Suggestions for the Community Platform
- 4.8K Certification Community
- 4.6K Certification Community Discussions
- 21 Oracle Certified Master Profiles
- 29 Oracle Database 12c Administrator Certified Master Profiles
- 72 Visual Builder Cloud Service
The Java Mail IdleManager listened for a few minutes and then it didn't work. Please help me, thank

Dear engineer, I have a problem. I want to ask a question. I want my Java mail Idlemanager to start monitoring emails, but after a while I can't work. I found that I didn't exercise for more than 4 minutes. It is no longer valid. I want to ask how to keep a persistent link. Is there a problem with my code? Here is my code。 My version is 1.6.2, I am in trouble.
public static void main(String[] args) {
try {
SpringApplication.run(DemoApplication.class, args);
ExecutorService es = Executors.newCachedThreadPool();
String pop3Server = "imaphm.qiye.163.com";
String protocol = "imap";
int port = 143;
String username = "...";
String password = "...."; //
Properties props = new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.host", pop3Server); //
props.setProperty("mail.imap.usesocketchannels", "true");
props.put("mail.event.executor", es);
// 获取连接
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
// 获取Store对象
Store store = session.getStore(protocol);
store.connect(pop3Server, port, username, password);
final IdleManager idleManager = new IdleManager(session, es);
IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// folder.idle(false);
folder.getMessages();
System.out.println("running status->" + idleManager.isRunning());
folder.addMessageCountListener(new MessageCountAdapter() {
@Override
public void messagesAdded(MessageCountEvent e) {
System.out.println(" code send ");
super.messagesAdded(e);
Folder folder = (Folder) e.getSource();
Message[] msgs = e.getMessages();
for (int i = 0; i < msgs.length; i++) {
try {
if (!msgs[i].getFolder().isOpen()) {
//判断是否open
msgs[i].getFolder().open(Folder.READ_WRITE);
}
System.out.println("add 邮件" + "add 邮件主题" + msgs[i].getSubject());
StringBuffer content = new StringBuffer(30);
contentProcess(msgs[i], content);
System.out.println("邮件正文:" + (content.length() > 100 ? content.substring(0, 100) + "..." : content));
System.out.println("------------------第" + msgs[i].getMessageNumber() + "封邮件解析结束-------------------- ");
System.out.println("message" + msgs[i].getSubject());
System.out.println("connect" + msgs[i].getContent());
} catch (Exception e1) {
e1.printStackTrace();
}
try {
idleManager.watch(folder);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
idleManager.watch(folder);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void contentProcess(Part part, StringBuffer content) throws Exception {
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && !isContainTextAttach) {
content.append(part.getContent().toString());
} else if (part.isMimeType("message/rfc822")) {
contentProcess((Part) part.getContent(), content);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
contentProcess(bodyPart, content);
}
}
}