KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uidmsgshow


1 /*
2  * @(#)uidmsgshow.java 1.6 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.activation.*;
44
45 /*
46  * Demo app that exercises the Message interfaces.
47  * Access message given its UID.
48  * Show information about and contents of messages.
49  *
50  * @author John Mani
51  * @author Bill Shannon
52  */

53
54 public class uidmsgshow {
55
56     static String JavaDoc protocol;
57     static String JavaDoc host = null;
58     static String JavaDoc user = null;
59     static String JavaDoc password = null;
60     static String JavaDoc mbox = "INBOX";
61     static String JavaDoc url = null;
62     static boolean verbose = false;
63
64     public static void main(String JavaDoc argv[]) {
65     long uid = -1;
66     int optind;
67
68     for (optind = 0; optind < argv.length; optind++) {
69         if (argv[optind].equals("-T")) {
70         protocol = argv[++optind];
71         } else if (argv[optind].equals("-H")) {
72         host = argv[++optind];
73         } else if (argv[optind].equals("-U")) {
74         user = argv[++optind];
75         } else if (argv[optind].equals("-P")) {
76         password = argv[++optind];
77         } else if (argv[optind].equals("-v")) {
78         verbose = true;
79         } else if (argv[optind].equals("-f")) {
80         mbox = argv[++optind];
81         } else if (argv[optind].equals("-L")) {
82         url = argv[++optind];
83         } else if (argv[optind].equals("--")) {
84         optind++;
85         break;
86         } else if (argv[optind].startsWith("-")) {
87         System.out.println("Usage: uidmsgshow [-L url] [-T protocol] [-H host] [-U user] [-P password] [-f mailbox] [uid] [-v]");
88         System.exit(1);
89         } else {
90         break;
91         }
92     }
93
94         try {
95         if (optind < argv.length)
96              uid = Long.parseLong(argv[optind]);
97
98         // Get a Properties object
99
Properties props = System.getProperties();
100
101         // Get a Session object
102
Session session = Session.getInstance(props, null);
103         // session.setDebug(true);
104

105         // Get a Store object
106
Store store = null;
107         if (url != null) {
108         URLName urln = new URLName(url);
109         store = session.getStore(urln);
110         store.connect();
111         } else {
112         if (protocol != null)
113             store = session.getStore(protocol);
114         else
115             store = session.getStore();
116
117         // Connect
118
if (host != null || user != null || password != null)
119             store.connect(host, user, password);
120         else
121             store.connect();
122         }
123         
124
125         // Open the Folder
126

127         Folder folder = store.getDefaultFolder();
128         if (folder == null) {
129             System.out.println("No default folder");
130             System.exit(1);
131         }
132
133         folder = folder.getFolder(mbox);
134         if (!folder.exists()) {
135             System.out.println(mbox + " does not exist");
136             System.exit(1);
137         }
138
139         if (!(folder instanceof UIDFolder)) {
140             System.out.println(
141             "This Provider or this folder does not support UIDs");
142             System.exit(1);
143         }
144
145         UIDFolder ufolder = (UIDFolder)folder;
146
147         folder.open(Folder.READ_WRITE);
148         int totalMessages = folder.getMessageCount();
149
150         if (totalMessages == 0) {
151         System.out.println("Empty folder");
152         folder.close(false);
153         store.close();
154         System.exit(1);
155         }
156
157         if (verbose) {
158         int newMessages = folder.getNewMessageCount();
159         System.out.println("Total messages = " + totalMessages);
160         System.out.println("New messages = " + newMessages);
161         System.out.println("-------------------------------");
162         }
163
164         if (uid == -1) {
165         // Attributes & Flags for ALL messages ..
166
Message[] msgs =
167             ufolder.getMessagesByUID(1, UIDFolder.LASTUID);
168
169         // Use a suitable FetchProfile
170
FetchProfile fp = new FetchProfile();
171         fp.add(FetchProfile.Item.ENVELOPE);
172         fp.add(FetchProfile.Item.FLAGS);
173         fp.add("X-Mailer");
174         folder.fetch(msgs, fp);
175
176         for (int i = 0; i < msgs.length; i++) {
177             System.out.println("--------------------------");
178             System.out.println("MESSAGE UID #" +
179                     ufolder.getUID(msgs[i]) + ":");
180             dumpEnvelope(msgs[i]);
181             // dumpPart(msgs[i]);
182
}
183         } else {
184         System.out.println("Getting message UID: " + uid);
185         Message m = ufolder.getMessageByUID(uid);
186         if (m != null)
187             dumpPart(m);
188         else
189             System.out.println(
190             "This Message does not exist on this folder");
191         }
192
193         folder.close(false);
194         store.close();
195     } catch (Exception JavaDoc ex) {
196         System.out.println("Oops, got exception! " + ex.getMessage());
197         ex.printStackTrace();
198     }
199     System.exit(1);
200     }
201
202     public static void dumpPart(Part p) throws Exception JavaDoc {
203     if (p instanceof Message)
204         dumpEnvelope((Message)p);
205
206     /* Dump input stream
207     InputStream is = new BufferedInputStream(p.getInputStream());
208     int c;
209     while ((c = is.read()) != -1)
210         System.out.write(c);
211     */

212
213     System.out.println("CONTENT-TYPE: " + p.getContentType());
214
215     Object JavaDoc o = p.getContent();
216     if (o instanceof String JavaDoc) {
217         System.out.println("This is a String");
218         System.out.println("---------------------------");
219         System.out.println((String JavaDoc)o);
220     } else if (o instanceof Multipart) {
221         System.out.println("This is a Multipart");
222         System.out.println("---------------------------");
223         Multipart mp = (Multipart)o;
224         int count = mp.getCount();
225         for (int i = 0; i < count; i++)
226         dumpPart(mp.getBodyPart(i));
227     } else if (o instanceof Message) {
228         System.out.println("This is a Nested Message");
229         System.out.println("---------------------------");
230         dumpPart((Part)o);
231     } else if (o instanceof InputStream) {
232         System.out.println("This is just an input stream");
233         System.out.println("---------------------------");
234         InputStream is = (InputStream)o;
235         int c;
236         while ((c = is.read()) != -1)
237         System.out.write(c);
238     }
239     }
240
241     public static void dumpEnvelope(Message m) throws Exception JavaDoc {
242     System.out.println("This is the message envelope");
243     System.out.println("---------------------------");
244     Address[] a;
245     // FROM
246
if ((a = m.getFrom()) != null) {
247         for (int j = 0; j < a.length; j++)
248         System.out.println("FROM: " + a[j].toString());
249     }
250
251     // TO
252
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
253         for (int j = 0; j < a.length; j++)
254         System.out.println("TO: " + a[j].toString());
255     }
256
257     // SUBJECT
258
System.out.println("SUBJECT: " + m.getSubject());
259
260     // DATE
261
Date d = m.getSentDate();
262     System.out.println("SendDate: " +
263         (d != null ? d.toString() : "UNKNOWN"));
264
265     // SIZE
266
System.out.println("Size: " + m.getSize());
267
268     // FLAGS:
269
Flags flags = m.getFlags();
270     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
271     Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
272

273     boolean first = true;
274     for (int i = 0; i < sf.length; i++) {
275         String JavaDoc s;
276         Flags.Flag f = sf[i];
277         if (f == Flags.Flag.ANSWERED)
278         s = "\\Answered";
279         else if (f == Flags.Flag.DELETED)
280         s = "\\Deleted";
281         else if (f == Flags.Flag.DRAFT)
282         s = "\\Draft";
283         else if (f == Flags.Flag.FLAGGED)
284         s = "\\Flagged";
285         else if (f == Flags.Flag.RECENT)
286         s = "\\Recent";
287         else if (f == Flags.Flag.SEEN)
288         s = "\\Seen";
289         else
290         continue; // skip it
291
if (first)
292         first = false;
293         else
294         sb.append(' ');
295         sb.append(s);
296     }
297
298     String JavaDoc[] uf = flags.getUserFlags(); // get the user flag strings
299
for (int i = 0; i < uf.length; i++) {
300         if (first)
301         first = false;
302         else
303         sb.append(' ');
304         sb.append(uf[i]);
305     }
306     System.out.println("FLAGS = " + sb.toString());
307
308     // X-MAILER
309
String JavaDoc[] hdrs = m.getHeader("X-Mailer");
310     if (hdrs != null)
311         System.out.println("X-Mailer: " + hdrs[0]);
312     else
313         System.out.println("X-Mailer NOT available");
314     }
315 }
316
Popular Tags