1 38 39 import java.util.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.event.*; 43 import javax.activation.*; 44 45 46 47 public class monitor { 48 49 public static void main(String argv[]) 50 { 51 if (argv.length != 5) { 52 System.out.println("Usage: monitor <host> <user> <password> <mbox> <freq>"); 53 System.exit(1); 54 } 55 System.out.println("\nTesting monitor\n"); 56 57 try { 58 Properties props = System.getProperties(); 59 60 Session session = Session.getInstance(props, null); 62 64 Store store = session.getStore("imap"); 66 67 store.connect(argv[0], argv[1], argv[2]); 69 70 Folder folder = store.getFolder(argv[3]); 72 if (folder == null || !folder.exists()) { 73 System.out.println("Invalid folder"); 74 System.exit(1); 75 } 76 77 folder.open(Folder.READ_WRITE); 78 79 folder.addMessageCountListener(new MessageCountAdapter() { 81 public void messagesAdded(MessageCountEvent ev) { 82 Message[] msgs = ev.getMessages(); 83 System.out.println("Got " + msgs.length + " new messages"); 84 85 for (int i = 0; i < msgs.length; i++) { 87 try { 88 DataHandler dh = msgs[i].getDataHandler(); 89 InputStream is = dh.getInputStream(); 90 int c; 91 while ((c = is.read()) != -1) 92 System.out.write(c); 93 } catch (IOException ioex) { 94 ioex.printStackTrace(); 95 } catch (MessagingException mex) { 96 mex.printStackTrace(); 97 } 98 } 99 } 100 }); 101 102 104 int freq = Integer.parseInt(argv[4]); 105 106 for (; ;) { 107 Thread.sleep(freq); 109 folder.getMessageCount(); 112 } 113 114 } catch (Exception ex) { 115 ex.printStackTrace(); 116 } 117 } 118 } 119 | Popular Tags |