| 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.mail.search.*; |
| 16 |
import jakarta.activation.*; |
| 17 |
|
| 18 |
/* |
| 19 |
* Search the given folder for messages matching the given |
| 20 |
* criteria. |
| 21 |
* |
| 22 |
* @author John Mani |
| 23 |
*/ |
| 24 |
|
| 25 |
public class search { |
| 26 |
|
| 27 |
static String protocol = "imap"; |
| 28 |
static String host = null; |
| 29 |
static String user = null; |
| 30 |
static String password = null; |
| 31 |
static String mbox = "INBOX"; |
| 32 |
static String url = null; |
| 33 |
static boolean debug = false; |
| 34 |
|
| 35 |
public static void main(String argv[]) { |
| 36 |
int optind; |
| 37 |
|
| 38 |
String subject = null; |
| 39 |
String from = null; |
| 40 |
boolean or = false; |
| 41 |
boolean today = false; |
| 42 |
int size = -1; |
| 43 |
|
| 44 |
for (optind = 0; optind < argv.length; optind++) { |
| 45 |
if (argv[optind].equals("-T")) { |
| 46 |
protocol = argv[++optind]; |
| 47 |
} else if (argv[optind].equals("-H")) { |
| 48 |
host = argv[++optind]; |
| 49 |
} else if (argv[optind].equals("-U")) { |
| 50 |
user = argv[++optind]; |
| 51 |
} else if (argv[optind].equals("-P")) { |
| 52 |
password = argv[++optind]; |
| 53 |
} else if (argv[optind].equals("-or")) { |
| 54 |
or = true; |
| 55 |
} else if (argv[optind].equals("-D")) { |
| 56 |
debug = true; |
| 57 |
} else if (argv[optind].equals("-f")) { |
| 58 |
mbox = argv[++optind]; |
| 59 |
} else if (argv[optind].equals("-L")) { |
| 60 |
url = argv[++optind]; |
| 61 |
} else if (argv[optind].equals("-subject")) { |
| 62 |
subject = argv[++optind]; |
| 63 |
} else if (argv[optind].equals("-from")) { |
| 64 |
from = argv[++optind]; |
| 65 |
} else if (argv[optind].equals("-today")) { |
| 66 |
today = true; |
| 67 |
} else if (argv[optind].equals("-size")) { |
| 68 |
size = Integer.parseInt(argv[++optind]); |
| 69 |
} else if (argv[optind].equals("--")) { |
| 70 |
optind++; |
| 71 |
break; |
| 72 |
} else if (argv[optind].startsWith("-")) { |
| 73 |
System.out.println( |
| 74 |
"Usage: search [-D] [-L url] [-T protocol] [-H host] " + |
| 75 |
"[-U user] [-P password] [-f mailbox] " + |
| 76 |
"[-subject subject] [-from from] [-or] [-today]"); |
| 77 |
System.exit(1); |
| 78 |
} else { |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
try { |
| 84 |
|
| 85 |
if ((subject == null) && (from == null) && !today && size < 0) { |
| 86 |
System.out.println( |
| 87 |
"Specify either -subject, -from, -today, or -size"); |
| 88 |
System.exit(1); |
| 89 |
} |
| 90 |
|
| 91 |
// Get a Properties object |
| 92 |
Properties props = System.getProperties(); |
| 93 |
|
| 94 |
// Get a Session object |
| 95 |
Session session = Session.getInstance(props, null); |
| 96 |
session.setDebug(debug); |
| 97 |
|
| 98 |
// Get a Store object |
| 99 |
Store store = null; |
| 100 |
if (url != null) { |
| 101 |
URLName urln = new URLName(url); |
| 102 |
store = session.getStore(urln); |
| 103 |
store.connect(); |
| 104 |
} else { |
| 105 |
if (protocol != null) |
| 106 |
store = session.getStore(protocol); |
| 107 |
else |
| 108 |
store = session.getStore(); |
| 109 |
|
| 110 |
// Connect |
| 111 |
if (host != null || user != null || password != null) |
| 112 |
store.connect(host, user, password); |
| 113 |
else |
| 114 |
store.connect(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
// Open the Folder |
| 119 |
|
| 120 |
Folder folder = store.getDefaultFolder(); |
| 121 |
if (folder == null) { |
| 122 |
System.out.println("Cant find default namespace"); |
| 123 |
System.exit(1); |
| 124 |
} |
| 125 |
|
| 126 |
folder = folder.getFolder(mbox); |
| 127 |
if (folder == null) { |
| 128 |
System.out.println("Invalid folder"); |
| 129 |
System.exit(1); |
| 130 |
} |
| 131 |
|
| 132 |
folder.open(Folder.READ_ONLY); |
| 133 |
SearchTerm term = null; |
| 134 |
|
| 135 |
if (subject != null) |
| 136 |
term = new SubjectTerm(subject); |
| 137 |
if (from != null) { |
| 138 |
FromStringTerm fromTerm = new FromStringTerm(from); |
| 139 |
if (term != null) { |
| 140 |
if (or) |
| 141 |
term = new OrTerm(term, fromTerm); |
| 142 |
else |
| 143 |
term = new AndTerm(term, fromTerm); |
| 144 |
} |
| 145 |
else |
| 146 |
term = fromTerm; |
| 147 |
} |
| 148 |
if (today) { |
| 149 |
Calendar c = Calendar.getInstance(); |
| 150 |
c.set(Calendar.HOUR, 0); |
| 151 |
c.set(Calendar.MINUTE, 0); |
| 152 |
c.set(Calendar.SECOND, 0); |
| 153 |
c.set(Calendar.MILLISECOND, 0); |
| 154 |
c.set(Calendar.AM_PM, Calendar.AM); |
| 155 |
ReceivedDateTerm startDateTerm = |
| 156 |
new ReceivedDateTerm(ComparisonTerm.GE, c.getTime()); |
| 157 |
c.add(Calendar.DATE, 1); // next day |
| 158 |
ReceivedDateTerm endDateTerm = |
| 159 |
new ReceivedDateTerm(ComparisonTerm.LT, c.getTime()); |
| 160 |
SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm); |
| 161 |
if (term != null) { |
| 162 |
if (or) |
| 163 |
term = new OrTerm(term, dateTerm); |
| 164 |
else |
| 165 |
term = new AndTerm(term, dateTerm); |
| 166 |
} |
| 167 |
else |
| 168 |
term = dateTerm; |
| 169 |
} |
| 170 |
|
| 171 |
if (size >= 0) { |
| 172 |
SizeTerm sizeTerm = new SizeTerm(ComparisonTerm.GT, size); |
| 173 |
if (term != null) { |
| 174 |
if (or) |
| 175 |
term = new OrTerm(term, sizeTerm); |
| 176 |
else |
| 177 |
term = new AndTerm(term, sizeTerm); |
| 178 |
} |
| 179 |
else |
| 180 |
term = sizeTerm; |
| 181 |
} |
| 182 |
|
| 183 |
Message[] msgs = folder.search(term); |
| 184 |
System.out.println("FOUND " + msgs.length + " MESSAGES"); |
| 185 |
if (msgs.length == 0) // no match |
| 186 |
System.exit(1); |
| 187 |
|
| 188 |
// Use a suitable FetchProfile |
| 189 |
FetchProfile fp = new FetchProfile(); |
| 190 |
fp.add(FetchProfile.Item.ENVELOPE); |
| 191 |
folder.fetch(msgs, fp); |
| 192 |
|
| 193 |
for (int i = 0; i < msgs.length; i++) { |
| 194 |
System.out.println("--------------------------"); |
| 195 |
System.out.println("MESSAGE #" + (i + 1) + ":"); |
| 196 |
dumpPart(msgs[i]); |
| 197 |
} |
| 198 |
|
| 199 |
folder.close(false); |
| 200 |
store.close(); |
| 201 |
} catch (Exception ex) { |
| 202 |
System.out.println("Oops, got exception! " + ex.getMessage()); |
| 203 |
ex.printStackTrace(); |
| 204 |
} |
| 205 |
|
| 206 |
System.exit(1); |
| 207 |
} |
| 208 |
|
| 209 |
public static void dumpPart(Part p) throws Exception { |
| 210 |
if (p instanceof Message) { |
| 211 |
Message m = (Message)p; |
| 212 |
Address[] a; |
| 213 |
// FROM |
| 214 |
if ((a = m.getFrom()) != null) { |
| 215 |
for (int j = 0; j < a.length; j++) |
| 216 |
System.out.println("FROM: " + a[j].toString()); |
| 217 |
} |
| 218 |
|
| 219 |
// TO |
| 220 |
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { |
| 221 |
for (int j = 0; j < a.length; j++) |
| 222 |
System.out.println("TO: " + a[j].toString()); |
| 223 |
} |
| 224 |
|
| 225 |
// SUBJECT |
| 226 |
System.out.println("SUBJECT: " + m.getSubject()); |
| 227 |
|
| 228 |
// DATE |
| 229 |
Date d = m.getSentDate(); |
| 230 |
System.out.println("SendDate: " + |
| 231 |
(d != null ? d.toLocaleString() : "UNKNOWN")); |
| 232 |
|
| 233 |
// FLAGS: |
| 234 |
Flags flags = m.getFlags(); |
| 235 |
StringBuffer sb = new StringBuffer(); |
| 236 |
Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags |
| 237 |
|
| 238 |
boolean first = true; |
| 239 |
for (int i = 0; i < sf.length; i++) { |
| 240 |
String s; |
| 241 |
Flags.Flag f = sf[i]; |
| 242 |
if (f == Flags.Flag.ANSWERED) |
| 243 |
s = "\\Answered"; |
| 244 |
else if (f == Flags.Flag.DELETED) |
| 245 |
s = "\\Deleted"; |
| 246 |
else if (f == Flags.Flag.DRAFT) |
| 247 |
s = "\\Draft"; |
| 248 |
else if (f == Flags.Flag.FLAGGED) |
| 249 |
s = "\\Flagged"; |
| 250 |
else if (f == Flags.Flag.RECENT) |
| 251 |
s = "\\Recent"; |
| 252 |
else if (f == Flags.Flag.SEEN) |
| 253 |
s = "\\Seen"; |
| 254 |
else |
| 255 |
continue; // skip it |
| 256 |
if (first) |
| 257 |
first = false; |
| 258 |
else |
| 259 |
sb.append(' '); |
| 260 |
sb.append(s); |
| 261 |
} |
| 262 |
|
| 263 |
String[] uf = flags.getUserFlags(); // get the user flag strings |
| 264 |
for (int i = 0; i < uf.length; i++) { |
| 265 |
if (first) |
| 266 |
first = false; |
| 267 |
else |
| 268 |
sb.append(' '); |
| 269 |
sb.append(uf[i]); |
| 270 |
} |
| 271 |
System.out.println("FLAGS = " + sb.toString()); |
| 272 |
} |
| 273 |
|
| 274 |
System.out.println("CONTENT-TYPE: " + p.getContentType()); |
| 275 |
|
| 276 |
/* Dump input stream |
| 277 |
InputStream is = ((MimeMessage)m).getInputStream(); |
| 278 |
int c; |
| 279 |
while ((c = is.read()) != -1) |
| 280 |
System.out.write(c); |
| 281 |
*/ |
| 282 |
|
| 283 |
Object o = p.getContent(); |
| 284 |
if (o instanceof String) { |
| 285 |
System.out.println("This is a String"); |
| 286 |
System.out.println((String)o); |
| 287 |
} else if (o instanceof Multipart) { |
| 288 |
System.out.println("This is a Multipart"); |
| 289 |
Multipart mp = (Multipart)o; |
| 290 |
int count = mp.getCount(); |
| 291 |
for (int i = 0; i < count; i++) |
| 292 |
dumpPart(mp.getBodyPart(i)); |
| 293 |
} else if (o instanceof InputStream) { |
| 294 |
System.out.println("This is just an input stream"); |
| 295 |
InputStream is = (InputStream)o; |
| 296 |
int c; |
| 297 |
while ((c = is.read()) != -1) |
| 298 |
System.out.write(c); |
| 299 |
} |
| 300 |
} |
| 301 |
} |