List of usage examples for javax.mail.event MessageCountAdapter MessageCountAdapter
MessageCountAdapter
From source file:monitor.java
public static void main(String argv[]) { if (argv.length != 5) { System.out.println("Usage: monitor <host> <user> <password> <mbox> <freq>"); System.exit(1);//from w w w .ja va 2 s. c o m } System.out.println("\nTesting monitor\n"); try { Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // session.setDebug(true); // Get a Store object Store store = session.getStore("imap"); // Connect store.connect(argv[0], argv[1], argv[2]); // Open a Folder Folder folder = store.getFolder(argv[3]); if (folder == null || !folder.exists()) { System.out.println("Invalid folder"); System.exit(1); } folder.open(Folder.READ_WRITE); // Add messageCountListener to listen for new messages folder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { Message[] msgs = ev.getMessages(); System.out.println("Got " + msgs.length + " new messages"); // Just dump out the new messages for (int i = 0; i < msgs.length; i++) { try { System.out.println("-----"); System.out.println("Message " + msgs[i].getMessageNumber() + ":"); msgs[i].writeTo(System.out); } catch (IOException ioex) { ioex.printStackTrace(); } catch (MessagingException mex) { mex.printStackTrace(); } } } }); // Check mail once in "freq" MILLIseconds int freq = Integer.parseInt(argv[4]); boolean supportsIdle = false; try { if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); supportsIdle = true; } } catch (FolderClosedException fex) { throw fex; } catch (MessagingException mex) { supportsIdle = false; } for (;;) { if (supportsIdle && folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); System.out.println("IDLE done"); } else { Thread.sleep(freq); // sleep for freq milliseconds // This is to force the IMAP server to send us // EXISTS notifications. folder.getMessageCount(); } } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:jp.mamesoft.mailsocketchat.Mail.java
public void run() { while (true) { try {/*from w w w . j a v a 2s .c om*/ System.out.println("????"); Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // session.setDebug(true); // Get a Store object Store store = session.getStore("imaps"); // Connect store.connect("imap.gmail.com", 993, Mailsocketchat.mail_user, Mailsocketchat.mail_pass); System.out.println("??????"); // Open a Folder Folder folder = store.getFolder("INBOX"); if (folder == null || !folder.exists()) { System.out.println("IMAP??????"); System.exit(1); } folder.open(Folder.READ_WRITE); // Add messageCountListener to listen for new messages folder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { Message[] msgs = ev.getMessages(); // Just dump out the new messages for (int i = 0; i < msgs.length; i++) { try { System.out.println("?????"); InternetAddress addrfrom = (InternetAddress) msgs[i].getFrom()[0]; String subject = msgs[i].getSubject(); if (subject == null) { subject = ""; } Pattern hashtag_p = Pattern.compile("#(.+)"); Matcher hashtag_m = hashtag_p.matcher(subject); if (subject.equals("#")) { hashtag = null; } else if (hashtag_m.find()) { hashtag = hashtag_m.group(1); } String comment = msgs[i].getContent().toString().replaceAll("\r\n", " "); comment = comment.replaceAll("\n", " "); comment = comment.replaceAll("\r", " "); JSONObject data = new JSONObject(); data.put("comment", comment); if (hashtag != null) { data.put("channel", hashtag); } if (!comment.equals("") && !comment.equals(" ") && !comment.equals(" ")) { Mailsocketchat.socket.emit("say", data); System.out.println("????"); } // if (subject.equals("push") || subject.equals("Push") || subject.equals("")) { Send(addrfrom.getAddress(), 2); Mailsocketchat.push = true; Mailsocketchat.repeat = false; Mailsocketchat.address = addrfrom.getAddress(); repeatthread.cancel(); repeatthread = null; System.out.println("?????"); } else if (subject.equals("fetch") || subject.equals("Fetch") || subject.equals("?")) { Send(addrfrom.getAddress(), 3); Mailsocketchat.push = false; Mailsocketchat.repeat = false; repeatthread.cancel(); repeatthread = null; System.out.println("??????"); } else if (subject.equals("repeat") || subject.equals("Repeat") || subject.equals("")) { Send(addrfrom.getAddress(), 7); Mailsocketchat.push = false; Mailsocketchat.repeat = true; Mailsocketchat.address = addrfrom.getAddress(); if (repeatthread == null) { repeatthread = new Repeat(); repeat = new Timer(); } repeat.schedule(repeatthread, 0, 30 * 1000); System.out.println("?????"); } else if (subject.equals("list") || subject.equals("List") || subject.equals("")) { Send(addrfrom.getAddress(), 4); } else if (subject.equals("help") || subject.equals("Help") || subject.equals("")) { Send(addrfrom.getAddress(), 5); } else { if (!Mailsocketchat.push && !Mailsocketchat.repeat) { Send(addrfrom.getAddress(), 0); } else if (comment.equals("") || comment.equals(" ") || comment.equals(" ")) { Send(addrfrom.getAddress(), 5); } } } catch (IOException ioex) { System.out.println( "??????????"); } catch (MessagingException mex) { System.out.println( "??????????"); } } } }); // Check mail once in "freq" MILLIseconds int freq = 1000; //?? boolean supportsIdle = false; try { if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); supportsIdle = true; } } catch (FolderClosedException fex) { throw fex; } catch (MessagingException mex) { supportsIdle = false; } for (;;) { if (supportsIdle && folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); } else { Thread.sleep(freq); // sleep for freq milliseconds // This is to force the IMAP server to send us // EXISTS notifications. folder.getMessageCount(); } } } catch (Exception ex) { System.out.println("??????????"); } } }
From source file:com.stimulus.archiva.incoming.IAPRunnable.java
public void imapIdleMessages() { logger.debug("iap imapidlemessages() {mailboxworker='" + getName() + "'}"); inboxFolder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { Message[] messages = null;/* w ww . j a v a 2s .com*/ try { if (store.isConnected()) { messages = ev.getMessages(); } else { return; } } catch (IllegalStateException ise) { logger.error("mail server inbox folder is not opened {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); testOutput("mail server inbox folder is not opened {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); return; } catch (IndexOutOfBoundsException iobe) { logger.error("mail server start and end message numbers are out of range {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); testOutput("mail server start and end message numbers are out of range {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); return; } catch (Throwable e) { logger.error("error occurred while idling on IMAP folder:" + e.getMessage() + " {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); } if (messages.length < 1) { logger.debug("idle messages: no messages to process"); return; } archiveMessages(messages); } }); boolean complete = false; long safety = 0; while (!complete && safety < 100000000) { complete = pollMessages(); safety++; } while (!shutdown && inboxFolder.isOpen()) { try { logger.debug("idling to IMAP server {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); ((IMAPFolder) inboxFolder).idle(); logger.debug("return from idling with IMAP server {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}"); } catch (FolderClosedException fce) { logger.debug("imap folder closed. possibly due to server timeout."); break; } catch (java.lang.IllegalStateException se) { logger.debug("idling has stopped. it is likely that the mailbox connection was shutdown:" + se.getMessage()); break; } catch (MessagingException me) { logger.error("failed to execute IMAP idle {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}", me); break; } catch (Throwable t) { logger.error("hard error occurred while executing IMAP idle {serverName='" + connection.getServerName() + "',port='" + connection.getPort() + "',userName='" + connection.getUsername() + "'}", t); break; } } logger.debug("imap idling loop exit {shutdown='" + shutdown + "'}"); }