KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > monitor


1 /*
2  * @(#)monitor.java 1.8 03/04/22
3  *
4  * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.util.*;
40 import java.io.*;
41 import javax.mail.*;
42 import javax.mail.event.*;
43 import javax.activation.*;
44
45 /* Monitors given mailbox for new mail */
46
47 public class monitor {
48
49     public static void main(String JavaDoc 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         // Get a Session object
61
Session session = Session.getInstance(props, null);
62         // session.setDebug(true);
63

64         // Get a Store object
65
Store store = session.getStore("imap");
66
67        // Connect
68
store.connect(argv[0], argv[1], argv[2]);
69
70        // Open a Folder
71
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         // Add messageCountListener to listen for new messages
80
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             // Just dump out the new messages
86
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        // Check mail once in "freq" MILLIseconds
103

104         int freq = Integer.parseInt(argv[4]);
105
106         for (; ;) {
107         Thread.sleep(freq); // sleep for freq milliseconds
108

109         // This is to force the IMAP server to send us
110
// EXISTS notifications.
111
folder.getMessageCount();
112         }
113
114     } catch (Exception JavaDoc ex) {
115         ex.printStackTrace();
116     }
117     }
118 }
119
Popular Tags