Debian Codegraph @

demo/src/main/java/uidmsgshow.java

raw
1 /*
2 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 import java.util.*;
12 import java.io.*;
13 import jakarta.mail.*;
14 import jakarta.mail.internet.*;
15 import jakarta.activation.*;
16
17 /*
18 * Demo app that exercises the Message interfaces.
19 * Access message given its UID.
20 * Show information about and contents of messages.
21 *
22 * @author John Mani
23 * @author Bill Shannon
24 */
25
26 public class uidmsgshow {
27
28 static String protocol;
29 static String host = null;
30 static String user = null;
31 static String password = null;
32 static String mbox = "INBOX";
33 static String url = null;
34 static boolean verbose = false;
35
36 public static void main(String argv[]) {
37 long uid = -1;
38 int optind;
39
40 for (optind = 0; optind < argv.length; optind++) {
41 if (argv[optind].equals("-T")) {
42 protocol = argv[++optind];
43 } else if (argv[optind].equals("-H")) {
44 host = argv[++optind];
45 } else if (argv[optind].equals("-U")) {
46 user = argv[++optind];
47 } else if (argv[optind].equals("-P")) {
48 password = argv[++optind];
49 } else if (argv[optind].equals("-v")) {
50 verbose = true;
51 } else if (argv[optind].equals("-f")) {
52 mbox = argv[++optind];
53 } else if (argv[optind].equals("-L")) {
54 url = argv[++optind];
55 } else if (argv[optind].equals("--")) {
56 optind++;
57 break;
58 } else if (argv[optind].startsWith("-")) {
59 System.out.println("Usage: uidmsgshow [-L url] [-T protocol] [-H host] [-U user] [-P password] [-f mailbox] [uid] [-v]");
60 System.exit(1);
61 } else {
62 break;
63 }
64 }
65
66 try {
67 if (optind < argv.length)
68 uid = Long.parseLong(argv[optind]);
69
70 // Get a Properties object
71 Properties props = System.getProperties();
72
73 // Get a Session object
74 Session session = Session.getInstance(props, null);
75 // session.setDebug(true);
76
77 // Get a Store object
78 Store store = null;
79 if (url != null) {
80 URLName urln = new URLName(url);
81 store = session.getStore(urln);
82 store.connect();
83 } else {
84 if (protocol != null)
85 store = session.getStore(protocol);
86 else
87 store = session.getStore();
88
89 // Connect
90 if (host != null || user != null || password != null)
91 store.connect(host, user, password);
92 else
93 store.connect();
94 }
95
96
97 // Open the Folder
98
99 Folder folder = store.getDefaultFolder();
100 if (folder == null) {
101 System.out.println("No default folder");
102 System.exit(1);
103 }
104
105 folder = folder.getFolder(mbox);
106 if (!folder.exists()) {
107 System.out.println(mbox + " does not exist");
108 System.exit(1);
109 }
110
111 if (!(folder instanceof UIDFolder)) {
112 System.out.println(
113 "This Provider or this folder does not support UIDs");
114 System.exit(1);
115 }
116
117 UIDFolder ufolder = (UIDFolder)folder;
118
119 folder.open(Folder.READ_WRITE);
120 int totalMessages = folder.getMessageCount();
121
122 if (totalMessages == 0) {
123 System.out.println("Empty folder");
124 folder.close(false);
125 store.close();
126 System.exit(1);
127 }
128
129 if (verbose) {
130 int newMessages = folder.getNewMessageCount();
131 System.out.println("Total messages = " + totalMessages);
132 System.out.println("New messages = " + newMessages);
133 System.out.println("-------------------------------");
134 }
135
136 if (uid == -1) {
137 // Attributes & Flags for ALL messages ..
138 Message[] msgs =
139 ufolder.getMessagesByUID(1, UIDFolder.LASTUID);
140
141 // Use a suitable FetchProfile
142 FetchProfile fp = new FetchProfile();
143 fp.add(FetchProfile.Item.ENVELOPE);
144 fp.add(FetchProfile.Item.FLAGS);
145 fp.add("X-Mailer");
146 folder.fetch(msgs, fp);
147
148 for (int i = 0; i < msgs.length; i++) {
149 System.out.println("--------------------------");
150 System.out.println("MESSAGE UID #" +
151 ufolder.getUID(msgs[i]) + ":");
152 dumpEnvelope(msgs[i]);
153 // dumpPart(msgs[i]);
154 }
155 } else {
156 System.out.println("Getting message UID: " + uid);
157 Message m = ufolder.getMessageByUID(uid);
158 if (m != null)
159 dumpPart(m);
160 else
161 System.out.println(
162 "This Message does not exist on this folder");
163 }
164
165 folder.close(false);
166 store.close();
167 } catch (Exception ex) {
168 System.out.println("Oops, got exception! " + ex.getMessage());
169 ex.printStackTrace();
170 }
171 System.exit(1);
172 }
173
174 public static void dumpPart(Part p) throws Exception {
175 if (p instanceof Message)
176 dumpEnvelope((Message)p);
177
178 /* Dump input stream
179 InputStream is = new BufferedInputStream(p.getInputStream());
180 int c;
181 while ((c = is.read()) != -1)
182 System.out.write(c);
183 */
184
185 System.out.println("CONTENT-TYPE: " + p.getContentType());
186
187 Object o = p.getContent();
188 if (o instanceof String) {
189 System.out.println("This is a String");
190 System.out.println("---------------------------");
191 System.out.println((String)o);
192 } else if (o instanceof Multipart) {
193 System.out.println("This is a Multipart");
194 System.out.println("---------------------------");
195 Multipart mp = (Multipart)o;
196 int count = mp.getCount();
197 for (int i = 0; i < count; i++)
198 dumpPart(mp.getBodyPart(i));
199 } else if (o instanceof Message) {
200 System.out.println("This is a Nested Message");
201 System.out.println("---------------------------");
202 dumpPart((Part)o);
203 } else if (o instanceof InputStream) {
204 System.out.println("This is just an input stream");
205 System.out.println("---------------------------");
206 InputStream is = (InputStream)o;
207 int c;
208 while ((c = is.read()) != -1)
209 System.out.write(c);
210 }
211 }
212
213 public static void dumpEnvelope(Message m) throws Exception {
214 System.out.println("This is the message envelope");
215 System.out.println("---------------------------");
216 Address[] a;
217 // FROM
218 if ((a = m.getFrom()) != null) {
219 for (int j = 0; j < a.length; j++)
220 System.out.println("FROM: " + a[j].toString());
221 }
222
223 // TO
224 if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
225 for (int j = 0; j < a.length; j++)
226 System.out.println("TO: " + a[j].toString());
227 }
228
229 // SUBJECT
230 System.out.println("SUBJECT: " + m.getSubject());
231
232 // DATE
233 Date d = m.getSentDate();
234 System.out.println("SendDate: " +
235 (d != null ? d.toString() : "UNKNOWN"));
236
237 // SIZE
238 System.out.println("Size: " + m.getSize());
239
240 // FLAGS:
241 Flags flags = m.getFlags();
242 StringBuffer sb = new StringBuffer();
243 Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
244
245 boolean first = true;
246 for (int i = 0; i < sf.length; i++) {
247 String s;
248 Flags.Flag f = sf[i];
249 if (f == Flags.Flag.ANSWERED)
250 s = "\\Answered";
251 else if (f == Flags.Flag.DELETED)
252 s = "\\Deleted";
253 else if (f == Flags.Flag.DRAFT)
254 s = "\\Draft";
255 else if (f == Flags.Flag.FLAGGED)
256 s = "\\Flagged";
257 else if (f == Flags.Flag.RECENT)
258 s = "\\Recent";
259 else if (f == Flags.Flag.SEEN)
260 s = "\\Seen";
261 else
262 continue; // skip it
263 if (first)
264 first = false;
265 else
266 sb.append(' ');
267 sb.append(s);
268 }
269
270 String[] uf = flags.getUserFlags(); // get the user flag strings
271 for (int i = 0; i < uf.length; i++) {
272 if (first)
273 first = false;
274 else
275 sb.append(' ');
276 sb.append(uf[i]);
277 }
278 System.out.println("FLAGS = " + sb.toString());
279
280 // X-MAILER
281 String[] hdrs = m.getHeader("X-Mailer");
282 if (hdrs != null)
283 System.out.println("X-Mailer: " + hdrs[0]);
284 else
285 System.out.println("X-Mailer NOT available");
286 }
287 }