Forum Stats

  • 3,815,783 Users
  • 2,259,081 Discussions
  • 7,893,229 Comments

Discussions

The Java Mail IdleManager listened for a few minutes and then it didn't work. Please help me, thank

4103887
4103887 Member Posts: 1
edited Oct 8, 2019 2:28PM in Social Groups

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);
   }

  }

  }