KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > search


1 /*
2  * @(#)search.java 1.14 03/04/22
3  *
4  * Copyright 1997-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.internet.*;
43 import javax.mail.search.*;
44 import javax.activation.*;
45
46 /*
47  * Search the given folder for messages matching the given
48  * criteria.
49  *
50  * @author John Mani
51  */

52
53 public class search {
54
55     static String JavaDoc protocol = "imap";
56     static String JavaDoc host = null;
57     static String JavaDoc user = null;
58     static String JavaDoc password = null;
59     static String JavaDoc mbox = "INBOX";
60     static String JavaDoc url = null;
61     static boolean debug = false;
62
63     public static void main(String JavaDoc argv[]) {
64     int optind;
65
66     String JavaDoc subject = null;
67     String JavaDoc from = null;
68         boolean or = false;
69     boolean today = false;
70
71     for (optind = 0; optind < argv.length; optind++) {
72         if (argv[optind].equals("-T")) {
73         protocol = argv[++optind];
74         } else if (argv[optind].equals("-H")) {
75         host = argv[++optind];
76         } else if (argv[optind].equals("-U")) {
77         user = argv[++optind];
78         } else if (argv[optind].equals("-P")) {
79         password = argv[++optind];
80         } else if (argv[optind].equals("-or")) {
81         or = true;
82         } else if (argv[optind].equals("-D")) {
83         debug = true;
84         } else if (argv[optind].equals("-f")) {
85         mbox = argv[++optind];
86         } else if (argv[optind].equals("-L")) {
87         url = argv[++optind];
88         } else if (argv[optind].equals("-subject")) {
89         subject = argv[++optind];
90         } else if (argv[optind].equals("-from")) {
91         from = argv[++optind];
92         } else if (argv[optind].equals("-today")) {
93         today = true;
94         } else if (argv[optind].equals("--")) {
95         optind++;
96         break;
97         } else if (argv[optind].startsWith("-")) {
98         System.out.println(
99          "Usage: search [-D] [-L url] [-T protocol] [-H host] " +
100          "[-U user] [-P password] [-f mailbox] " +
101          "[-subject subject] [-from from] [-or] [-today]");
102         System.exit(1);
103         } else {
104         break;
105         }
106     }
107
108         try {
109
110         if ((subject == null) && (from == null) && !today) {
111         System.out.println("Specify either -subject, -from or -today");
112         System.exit(1);
113         }
114
115         // Get a Properties object
116
Properties props = System.getProperties();
117
118         // Get a Session object
119
Session session = Session.getInstance(props, null);
120         session.setDebug(debug);
121
122         // Get a Store object
123
Store store = null;
124         if (url != null) {
125         URLName urln = new URLName(url);
126         store = session.getStore(urln);
127         store.connect();
128         } else {
129         if (protocol != null)
130             store = session.getStore(protocol);
131         else
132             store = session.getStore();
133
134         // Connect
135
if (host != null || user != null || password != null)
136             store.connect(host, user, password);
137         else
138             store.connect();
139         }
140         
141
142         // Open the Folder
143

144         Folder folder = store.getDefaultFolder();
145         if (folder == null) {
146             System.out.println("Cant find default namespace");
147             System.exit(1);
148         }
149
150         folder = folder.getFolder(mbox);
151         if (folder == null) {
152             System.out.println("Invalid folder");
153             System.exit(1);
154         }
155
156         folder.open(Folder.READ_ONLY);
157         SearchTerm term = null;
158
159         if (subject != null)
160         term = new SubjectTerm(subject);
161         if (from != null) {
162         FromStringTerm fromTerm = new FromStringTerm(from);
163         if (term != null) {
164             if (or)
165             term = new OrTerm(term, fromTerm);
166             else
167             term = new AndTerm(term, fromTerm);
168         }
169         else
170             term = fromTerm;
171         }
172         if (today) {
173         ReceivedDateTerm dateTerm =
174          new ReceivedDateTerm(ComparisonTerm.EQ, new Date());
175         if (term != null) {
176             if (or)
177             term = new OrTerm(term, dateTerm);
178             else
179             term = new AndTerm(term, dateTerm);
180         }
181         else
182             term = dateTerm;
183         }
184
185         Message[] msgs = folder.search(term);
186         System.out.println("FOUND " + msgs.length + " MESSAGES");
187         if (msgs.length == 0) // no match
188
System.exit(1);
189
190         // Use a suitable FetchProfile
191
FetchProfile fp = new FetchProfile();
192         fp.add(FetchProfile.Item.ENVELOPE);
193         folder.fetch(msgs, fp);
194
195         for (int i = 0; i < msgs.length; i++) {
196         System.out.println("--------------------------");
197         System.out.println("MESSAGE #" + (i + 1) + ":");
198         dumpPart(msgs[i]);
199         }
200
201         folder.close(false);
202         store.close();
203     } catch (Exception JavaDoc ex) {
204         System.out.println("Oops, got exception! " + ex.getMessage());
205         ex.printStackTrace();
206     }
207
208     System.exit(1);
209     }
210
211     public static void dumpPart(Part p) throws Exception JavaDoc {
212     if (p instanceof Message) {
213         Message m = (Message)p;
214         Address[] a;
215         // FROM
216
if ((a = m.getFrom()) != null) {
217         for (int j = 0; j < a.length; j++)
218             System.out.println("FROM: " + a[j].toString());
219         }
220
221         // TO
222
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
223         for (int j = 0; j < a.length; j++)
224             System.out.println("TO: " + a[j].toString());
225         }
226
227         // SUBJECT
228
System.out.println("SUBJECT: " + m.getSubject());
229
230         // DATE
231
Date d = m.getSentDate();
232         System.out.println("SendDate: " +
233         (d != null ? d.toLocaleString() : "UNKNOWN"));
234
235         // FLAGS:
236
Flags flags = m.getFlags();
237         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
238         Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
239

240         boolean first = true;
241         for (int i = 0; i < sf.length; i++) {
242         String JavaDoc s;
243         Flags.Flag f = sf[i];
244         if (f == Flags.Flag.ANSWERED)
245             s = "\\Answered";
246         else if (f == Flags.Flag.DELETED)
247             s = "\\Deleted";
248         else if (f == Flags.Flag.DRAFT)
249             s = "\\Draft";
250         else if (f == Flags.Flag.FLAGGED)
251             s = "\\Flagged";
252         else if (f == Flags.Flag.RECENT)
253             s = "\\Recent";
254         else if (f == Flags.Flag.SEEN)
255             s = "\\Seen";
256         else
257             continue; // skip it
258
if (first)
259             first = false;
260         else
261             sb.append(' ');
262         sb.append(s);
263         }
264
265         String JavaDoc[] uf = flags.getUserFlags(); // get the user flag strings
266
for (int i = 0; i < uf.length; i++) {
267         if (first)
268             first = false;
269         else
270             sb.append(' ');
271         sb.append(uf[i]);
272         }
273         System.out.println("FLAGS = " + sb.toString());
274     }
275
276     System.out.println("CONTENT-TYPE: " + p.getContentType());
277
278     /* Dump input stream
279     InputStream is = ((MimeMessage)m).getInputStream();
280     int c;
281     while ((c = is.read()) != -1)
282         System.out.write(c);
283     */

284
285     Object JavaDoc o = p.getContent();
286     if (o instanceof String JavaDoc) {
287         System.out.println("This is a String");
288         System.out.println((String JavaDoc)o);
289     } else if (o instanceof Multipart) {
290         System.out.println("This is a Multipart");
291         Multipart mp = (Multipart)o;
292         int count = mp.getCount();
293         for (int i = 0; i < count; i++)
294         dumpPart(mp.getBodyPart(i));
295     } else if (o instanceof InputStream) {
296         System.out.println("This is just an input stream");
297         InputStream is = (InputStream)o;
298         int c;
299         while ((c = is.read()) != -1)
300         System.out.write(c);
301     }
302     }
303 }
304
Popular Tags