1 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 52 53 public class search { 54 55 static String protocol = "imap"; 56 static String host = null; 57 static String user = null; 58 static String password = null; 59 static String mbox = "INBOX"; 60 static String url = null; 61 static boolean debug = false; 62 63 public static void main(String argv[]) { 64 int optind; 65 66 String subject = null; 67 String 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 Properties props = System.getProperties(); 117 118 Session session = Session.getInstance(props, null); 120 session.setDebug(debug); 121 122 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 if (host != null || user != null || password != null) 136 store.connect(host, user, password); 137 else 138 store.connect(); 139 } 140 141 142 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) System.exit(1); 189 190 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 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 { 212 if (p instanceof Message) { 213 Message m = (Message)p; 214 Address[] a; 215 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 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 System.out.println("SUBJECT: " + m.getSubject()); 229 230 Date d = m.getSentDate(); 232 System.out.println("SendDate: " + 233 (d != null ? d.toLocaleString() : "UNKNOWN")); 234 235 Flags flags = m.getFlags(); 237 StringBuffer sb = new StringBuffer (); 238 Flags.Flag[] sf = flags.getSystemFlags(); 240 boolean first = true; 241 for (int i = 0; i < sf.length; i++) { 242 String 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; if (first) 259 first = false; 260 else 261 sb.append(' '); 262 sb.append(s); 263 } 264 265 String [] uf = flags.getUserFlags(); 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 284 285 Object o = p.getContent(); 286 if (o instanceof String ) { 287 System.out.println("This is a String"); 288 System.out.println((String )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 |