1 38 39 import java.util.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.event.*; 43 import javax.mail.internet.*; 44 import javax.activation.*; 45 46 53 54 public class msgshow { 55 56 static String protocol; 57 static String host = null; 58 static String user = null; 59 static String password = null; 60 static String mbox = "INBOX"; 61 static String url = null; 62 static int port = -1; 63 static boolean verbose = false; 64 static boolean debug = false; 65 static boolean showStructure = false; 66 static boolean showMessage = false; 67 static boolean showAlert = false; 68 static boolean saveAttachments = false; 69 static int attnum = 1; 70 71 public static void main(String argv[]) { 72 int msgnum = -1; 73 int optind; 74 InputStream msgStream = System.in; 75 76 for (optind = 0; optind < argv.length; optind++) { 77 if (argv[optind].equals("-T")) { 78 protocol = argv[++optind]; 79 } else if (argv[optind].equals("-H")) { 80 host = argv[++optind]; 81 } else if (argv[optind].equals("-U")) { 82 user = argv[++optind]; 83 } else if (argv[optind].equals("-P")) { 84 password = argv[++optind]; 85 } else if (argv[optind].equals("-v")) { 86 verbose = true; 87 } else if (argv[optind].equals("-D")) { 88 debug = true; 89 } else if (argv[optind].equals("-f")) { 90 mbox = argv[++optind]; 91 } else if (argv[optind].equals("-L")) { 92 url = argv[++optind]; 93 } else if (argv[optind].equals("-p")) { 94 port = Integer.parseInt(argv[++optind]); 95 } else if (argv[optind].equals("-s")) { 96 showStructure = true; 97 } else if (argv[optind].equals("-S")) { 98 saveAttachments = true; 99 } else if (argv[optind].equals("-m")) { 100 showMessage = true; 101 } else if (argv[optind].equals("-a")) { 102 showAlert = true; 103 } else if (argv[optind].equals("--")) { 104 optind++; 105 break; 106 } else if (argv[optind].startsWith("-")) { 107 System.out.println( 108 "Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]"); 109 System.out.println( 110 "\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]"); 111 System.out.println( 112 "or msgshow -m [-v] [-D] [-s] [-S] < msg"); 113 System.exit(1); 114 } else { 115 break; 116 } 117 } 118 119 try { 120 if (optind < argv.length) 121 msgnum = Integer.parseInt(argv[optind]); 122 123 Properties props = System.getProperties(); 125 126 Session session = Session.getInstance(props, null); 128 session.setDebug(debug); 129 130 if (showMessage) { 131 MimeMessage msg = new MimeMessage(session, msgStream); 132 dumpPart(msg); 133 System.exit(0); 134 } 135 136 Store store = null; 138 if (url != null) { 139 URLName urln = new URLName(url); 140 store = session.getStore(urln); 141 if (showAlert) { 142 store.addStoreListener(new StoreListener() { 143 public void notification(StoreEvent e) { 144 String s; 145 if (e.getMessageType() == StoreEvent.ALERT) 146 s = "ALERT: "; 147 else 148 s = "NOTICE: "; 149 System.out.println(s + e.getMessage()); 150 } 151 }); 152 } 153 store.connect(); 154 } else { 155 if (protocol != null) 156 store = session.getStore(protocol); 157 else 158 store = session.getStore(); 159 160 if (host != null || user != null || password != null) 162 store.connect(host, port, user, password); 163 else 164 store.connect(); 165 } 166 167 168 170 Folder folder = store.getDefaultFolder(); 171 if (folder == null) { 172 System.out.println("No default folder"); 173 System.exit(1); 174 } 175 176 folder = folder.getFolder(mbox); 177 if (folder == null) { 178 System.out.println("Invalid folder"); 179 System.exit(1); 180 } 181 182 try { 184 folder.open(Folder.READ_WRITE); 185 } catch (MessagingException ex) { 186 folder.open(Folder.READ_ONLY); 187 } 188 int totalMessages = folder.getMessageCount(); 189 190 if (totalMessages == 0) { 191 System.out.println("Empty folder"); 192 folder.close(false); 193 store.close(); 194 System.exit(1); 195 } 196 197 if (verbose) { 198 int newMessages = folder.getNewMessageCount(); 199 System.out.println("Total messages = " + totalMessages); 200 System.out.println("New messages = " + newMessages); 201 System.out.println("-------------------------------"); 202 } 203 204 if (msgnum == -1) { 205 Message[] msgs = folder.getMessages(); 207 208 FetchProfile fp = new FetchProfile(); 210 fp.add(FetchProfile.Item.ENVELOPE); 211 fp.add(FetchProfile.Item.FLAGS); 212 fp.add("X-Mailer"); 213 folder.fetch(msgs, fp); 214 215 for (int i = 0; i < msgs.length; i++) { 216 System.out.println("--------------------------"); 217 System.out.println("MESSAGE #" + (i + 1) + ":"); 218 dumpEnvelope(msgs[i]); 219 } 221 } else { 222 System.out.println("Getting message number: " + msgnum); 223 Message m = null; 224 225 try { 226 m = folder.getMessage(msgnum); 227 dumpPart(m); 228 } catch (IndexOutOfBoundsException iex) { 229 System.out.println("Message number out of range"); 230 } 231 } 232 233 folder.close(false); 234 store.close(); 235 } catch (Exception ex) { 236 System.out.println("Oops, got exception! " + ex.getMessage()); 237 ex.printStackTrace(); 238 System.exit(1); 239 } 240 System.exit(0); 241 } 242 243 public static void dumpPart(Part p) throws Exception { 244 if (p instanceof Message) 245 dumpEnvelope((Message)p); 246 247 259 260 String ct = p.getContentType(); 261 try { 262 pr("CONTENT-TYPE: " + (new ContentType(ct)).toString()); 263 } catch (ParseException pex) { 264 pr("BAD CONTENT-TYPE: " + ct); 265 } 266 String filename = p.getFileName(); 267 if (filename != null) 268 pr("FILENAME: " + filename); 269 270 274 if (p.isMimeType("text/plain")) { 275 pr("This is plain text"); 276 pr("---------------------------"); 277 if (!showStructure && !saveAttachments) 278 System.out.println((String )p.getContent()); 279 } else if (p.isMimeType("multipart/*")) { 280 pr("This is a Multipart"); 281 pr("---------------------------"); 282 Multipart mp = (Multipart)p.getContent(); 283 level++; 284 int count = mp.getCount(); 285 for (int i = 0; i < count; i++) 286 dumpPart(mp.getBodyPart(i)); 287 level--; 288 } else if (p.isMimeType("message/rfc822")) { 289 pr("This is a Nested Message"); 290 pr("---------------------------"); 291 level++; 292 dumpPart((Part)p.getContent()); 293 level--; 294 } else { 295 if (!showStructure && !saveAttachments) { 296 300 Object o = p.getContent(); 301 if (o instanceof String ) { 302 pr("This is a string"); 303 pr("---------------------------"); 304 System.out.println((String )o); 305 } else if (o instanceof InputStream) { 306 pr("This is just an input stream"); 307 pr("---------------------------"); 308 InputStream is = (InputStream)o; 309 int c; 310 while ((c = is.read()) != -1) 311 System.out.write(c); 312 } else { 313 pr("This is an unknown type"); 314 pr("---------------------------"); 315 pr(o.toString()); 316 } 317 } else { 318 pr("---------------------------"); 320 } 321 } 322 323 329 if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) { 330 String disp = p.getDisposition(); 331 if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) { 333 if (filename == null) 334 filename = "Attachment" + attnum++; 335 pr("Saving attachment to file " + filename); 336 try { 337 File f = new File(filename); 338 if (f.exists()) 339 throw new IOException("file exists"); 341 OutputStream os = 342 new BufferedOutputStream(new FileOutputStream(f)); 343 InputStream is = p.getInputStream(); 344 int c; 345 while ((c = is.read()) != -1) 346 os.write(c); 347 os.close(); 348 } catch (IOException ex) { 349 pr("Failed to save attachment: " + ex); 350 } 351 pr("---------------------------"); 352 } 353 } 354 } 355 356 public static void dumpEnvelope(Message m) throws Exception { 357 pr("This is the message envelope"); 358 pr("---------------------------"); 359 Address[] a; 360 if ((a = m.getFrom()) != null) { 362 for (int j = 0; j < a.length; j++) 363 pr("FROM: " + a[j].toString()); 364 } 365 366 if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { 368 for (int j = 0; j < a.length; j++) 369 pr("TO: " + a[j].toString()); 370 } 371 372 pr("SUBJECT: " + m.getSubject()); 374 375 Date d = m.getSentDate(); 377 pr("SendDate: " + 378 (d != null ? d.toString() : "UNKNOWN")); 379 380 Flags flags = m.getFlags(); 382 StringBuffer sb = new StringBuffer (); 383 Flags.Flag[] sf = flags.getSystemFlags(); 385 boolean first = true; 386 for (int i = 0; i < sf.length; i++) { 387 String s; 388 Flags.Flag f = sf[i]; 389 if (f == Flags.Flag.ANSWERED) 390 s = "\\Answered"; 391 else if (f == Flags.Flag.DELETED) 392 s = "\\Deleted"; 393 else if (f == Flags.Flag.DRAFT) 394 s = "\\Draft"; 395 else if (f == Flags.Flag.FLAGGED) 396 s = "\\Flagged"; 397 else if (f == Flags.Flag.RECENT) 398 s = "\\Recent"; 399 else if (f == Flags.Flag.SEEN) 400 s = "\\Seen"; 401 else 402 continue; if (first) 404 first = false; 405 else 406 sb.append(' '); 407 sb.append(s); 408 } 409 410 String [] uf = flags.getUserFlags(); for (int i = 0; i < uf.length; i++) { 412 if (first) 413 first = false; 414 else 415 sb.append(' '); 416 sb.append(uf[i]); 417 } 418 pr("FLAGS: " + sb.toString()); 419 420 String [] hdrs = m.getHeader("X-Mailer"); 422 if (hdrs != null) 423 pr("X-Mailer: " + hdrs[0]); 424 else 425 pr("X-Mailer NOT available"); 426 } 427 428 static String indentStr = " "; 429 static int level = 0; 430 431 434 public static void pr(String s) { 435 if (showStructure) 436 System.out.print(indentStr.substring(0, level * 2)); 437 System.out.println(s); 438 } 439 } 440 | Popular Tags |