1 38 39 import java.util.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 import javax.activation.*; 44 45 53 54 public class uidmsgshow { 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 boolean verbose = false; 63 64 public static void main(String 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 Properties props = System.getProperties(); 100 101 Session session = Session.getInstance(props, null); 103 105 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 if (host != null || user != null || password != null) 119 store.connect(host, user, password); 120 else 121 store.connect(); 122 } 123 124 125 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 Message[] msgs = 167 ufolder.getMessagesByUID(1, UIDFolder.LASTUID); 168 169 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 } 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 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 { 203 if (p instanceof Message) 204 dumpEnvelope((Message)p); 205 206 212 213 System.out.println("CONTENT-TYPE: " + p.getContentType()); 214 215 Object o = p.getContent(); 216 if (o instanceof String ) { 217 System.out.println("This is a String"); 218 System.out.println("---------------------------"); 219 System.out.println((String )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 { 242 System.out.println("This is the message envelope"); 243 System.out.println("---------------------------"); 244 Address[] a; 245 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 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 System.out.println("SUBJECT: " + m.getSubject()); 259 260 Date d = m.getSentDate(); 262 System.out.println("SendDate: " + 263 (d != null ? d.toString() : "UNKNOWN")); 264 265 System.out.println("Size: " + m.getSize()); 267 268 Flags flags = m.getFlags(); 270 StringBuffer sb = new StringBuffer (); 271 Flags.Flag[] sf = flags.getSystemFlags(); 273 boolean first = true; 274 for (int i = 0; i < sf.length; i++) { 275 String 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; if (first) 292 first = false; 293 else 294 sb.append(' '); 295 sb.append(s); 296 } 297 298 String [] uf = flags.getUserFlags(); 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 String [] 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 |