Guest User

Untitled

a guest
Mar 4th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.57 KB | None | 0 0
  1. /*
  2. * @(#)msgshow.java 1.24 00/10/14
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7. * modify and redistribute this software in source and binary code form,
  8. * provided that i) this copyright notice and license appear on all copies of
  9. * the software; and ii) Licensee does not utilize the software in a manner
  10. * which is disparaging to Sun.
  11. *
  12. * This software is provided "AS IS," without a warranty of any kind. ALL
  13. * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14. * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15. * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16. * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17. * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18. * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19. * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20. * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21. * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22. * POSSIBILITY OF SUCH DAMAGES.
  23. *
  24. * This software is not designed or intended for use in on-line control of
  25. * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26. * the design, construction, operation or maintenance of any nuclear
  27. * facility. Licensee represents and warrants that it will not use or
  28. * redistribute the Software for such purposes.
  29. */
  30.  
  31. import java.util.*;
  32. import java.io.*;
  33. import javax.mail.*;
  34. import javax.mail.internet.*;
  35. import javax.activation.*;
  36.  
  37. /*
  38. * Demo app that exercises the Message interfaces.
  39. * Show information about and contents of messages.
  40. *
  41. * @author John Mani
  42. * @author Bill Shannon
  43. */
  44.  
  45. public class msgshow {
  46.  
  47. static String protocol;
  48. static String host = null;
  49. static String user = null;
  50. static String password = null;
  51. static String mbox = "INBOX";
  52. static String url = null;
  53. static int port = -1;
  54. static boolean verbose = false;
  55. static boolean debug = false;
  56. static boolean showStructure = false;
  57. static boolean showMessage = false;
  58.  
  59. public static void main(String argv[]) {
  60. int msgnum = -1;
  61. int optind;
  62.  
  63. for (optind = 0; optind < argv.length; optind++) {
  64. if (argv[optind].equals("-T")) {
  65. protocol = argv[++optind];
  66. } else if (argv[optind].equals("-H")) {
  67. host = argv[++optind];
  68. } else if (argv[optind].equals("-U")) {
  69. user = argv[++optind];
  70. } else if (argv[optind].equals("-P")) {
  71. password = argv[++optind];
  72. } else if (argv[optind].equals("-v")) {
  73. verbose = true;
  74. } else if (argv[optind].equals("-D")) {
  75. debug = true;
  76. } else if (argv[optind].equals("-f")) {
  77. mbox = argv[++optind];
  78. } else if (argv[optind].equals("-L")) {
  79. url = argv[++optind];
  80. } else if (argv[optind].equals("-p")) {
  81. port = Integer.parseInt(argv[++optind]);
  82. } else if (argv[optind].equals("-s")) {
  83. showStructure = true;
  84. } else if (argv[optind].equals("-m")) {
  85. showMessage = true;
  86. } else if (argv[optind].equals("--")) {
  87. optind++;
  88. break;
  89. } else if (argv[optind].startsWith("-")) {
  90. System.out.println(
  91. "Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
  92. System.out.println(
  93. "\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s]");
  94. System.out.println(
  95. "or msgshow -m [-v] [-D] [-s] < msg");
  96. System.exit(1);
  97. } else {
  98. break;
  99. }
  100. }
  101.  
  102. try {
  103. if (optind < argv.length)
  104. msgnum = Integer.parseInt(argv[optind]);
  105.  
  106. // Get a Properties object
  107. Properties props = System.getProperties();
  108.  
  109. // Get a Session object
  110. Session session = Session.getDefaultInstance(props, null);
  111. session.setDebug(debug);
  112.  
  113. if (showMessage) {
  114. MimeMessage msg = new MimeMessage(session, System.in);
  115. dumpPart(msg);
  116. System.exit(0);
  117. }
  118.  
  119. // Get a Store object
  120. Store store = null;
  121. if (url != null) {
  122. URLName urln = new URLName(url);
  123. store = session.getStore(urln);
  124. store.connect();
  125. } else {
  126. if (protocol != null)
  127. store = session.getStore(protocol);
  128. else
  129. store = session.getStore();
  130.  
  131. // Connect
  132. if (host != null || user != null || password != null)
  133. store.connect(host, port, user, password);
  134. else
  135. store.connect();
  136. }
  137.  
  138.  
  139. // Open the Folder
  140.  
  141. Folder folder = store.getDefaultFolder();
  142. if (folder == null) {
  143. System.out.println("No default folder");
  144. System.exit(1);
  145. }
  146.  
  147. folder = folder.getFolder(mbox);
  148. if (folder == null) {
  149. System.out.println("Invalid folder");
  150. System.exit(1);
  151. }
  152.  
  153. // try to open read/write and if that fails try read-only
  154. try {
  155. folder.open(Folder.READ_WRITE);
  156. } catch (MessagingException ex) {
  157. folder.open(Folder.READ_ONLY);
  158. }
  159. int totalMessages = folder.getMessageCount();
  160.  
  161. if (totalMessages == 0) {
  162. System.out.println("Empty folder");
  163. folder.close(false);
  164. store.close();
  165. System.exit(1);
  166. }
  167.  
  168. if (verbose) {
  169. int newMessages = folder.getNewMessageCount();
  170. System.out.println("Total messages = " + totalMessages);
  171. System.out.println("New messages = " + newMessages);
  172. System.out.println("-------------------------------");
  173. }
  174.  
  175. if (msgnum == -1) {
  176. // Attributes & Flags for all messages ..
  177. Message[] msgs = folder.getMessages();
  178.  
  179. // Use a suitable FetchProfile
  180. FetchProfile fp = new FetchProfile();
  181. fp.add(FetchProfile.Item.ENVELOPE);
  182. fp.add(FetchProfile.Item.FLAGS);
  183. fp.add("X-Mailer");
  184. folder.fetch(msgs, fp);
  185.  
  186. for (int i = 0; i < msgs.length; i++) {
  187. System.out.println("--------------------------");
  188. System.out.println("MESSAGE #" + (i + 1) + ":");
  189. dumpEnvelope(msgs[i]);
  190. // dumpPart(msgs[i]);
  191. }
  192. } else {
  193. System.out.println("Getting message number: " + msgnum);
  194. Message m = null;
  195.  
  196. try {
  197. m = folder.getMessage(msgnum);
  198. dumpPart(m);
  199. } catch (IndexOutOfBoundsException iex) {
  200. System.out.println("Message number out of range");
  201. }
  202. }
  203.  
  204. folder.close(false);
  205. store.close();
  206. } catch (Exception ex) {
  207. System.out.println("Oops, got exception! " + ex.getMessage());
  208. ex.printStackTrace();
  209. System.exit(1);
  210. }
  211. System.exit(0);
  212. }
  213.  
  214. public static void dumpPart(Part p) throws Exception {
  215. if (p instanceof Message)
  216. dumpEnvelope((Message)p);
  217.  
  218. /** Dump input stream ..
  219.  
  220. InputStream is = p.getInputStream();
  221. // If "is" is not already buffered, wrap a BufferedInputStream
  222. // around it.
  223. if (!(is instanceof BufferedInputStream))
  224. is = new BufferedInputStream(is);
  225. int c;
  226. while ((c = is.read()) != -1)
  227. System.out.write(c);
  228.  
  229. **/
  230.  
  231. pr("CONTENT-TYPE: " + p.getContentType());
  232. String filename = p.getFileName();
  233. if (filename != null)
  234. pr("FILENAME: " + filename);
  235.  
  236. /*
  237. * Using isMimeType to determine the content type avoids
  238. * fetching the actual content data until we need it.
  239. */
  240. if (p.isMimeType("text/plain")) {
  241. pr("This is plain text");
  242. pr("---------------------------");
  243. if (!showStructure)
  244. System.out.println((String)p.getContent());
  245. } else if (p.isMimeType("multipart/*")) {
  246. pr("This is a Multipart");
  247. pr("---------------------------");
  248. Multipart mp = (Multipart)p.getContent();
  249. level++;
  250. int count = mp.getCount();
  251. for (int i = 0; i < count; i++)
  252. dumpPart(mp.getBodyPart(i));
  253. level--;
  254. } else if (p.isMimeType("message/rfc822")) {
  255. pr("This is a Nested Message");
  256. pr("---------------------------");
  257. level++;
  258. dumpPart((Part)p.getContent());
  259. level--;
  260. } else if (!showStructure) {
  261. /*
  262. * If we actually want to see the data, and it's not a
  263. * MIME type we know, fetch it and check its Java type.
  264. */
  265. Object o = p.getContent();
  266. if (o instanceof String) {
  267. pr("This is a string");
  268. pr("---------------------------");
  269. System.out.println((String)o);
  270. } else if (o instanceof InputStream) {
  271. pr("This is just an input stream");
  272. pr("---------------------------");
  273. InputStream is = (InputStream)o;
  274. int c;
  275. while ((c = is.read()) != -1)
  276. System.out.write(c);
  277. } else {
  278. pr("This is an unknown type");
  279. pr("---------------------------");
  280. pr(o.toString());
  281. }
  282. } else {
  283. pr("This is an unknown type");
  284. pr("---------------------------");
  285. }
  286. }
  287.  
  288. public static void dumpEnvelope(Message m) throws Exception {
  289. pr("This is the message envelope");
  290. pr("---------------------------");
  291. Address[] a;
  292. // FROM
  293. if ((a = m.getFrom()) != null) {
  294. for (int j = 0; j < a.length; j++)
  295. pr("FROM: " + a[j].toString());
  296. }
  297.  
  298. // TO
  299. if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  300. for (int j = 0; j < a.length; j++)
  301. pr("TO: " + a[j].toString());
  302. }
  303.  
  304. // SUBJECT
  305. pr("SUBJECT: " + m.getSubject());
  306.  
  307. // DATE
  308. Date d = m.getSentDate();
  309. pr("SendDate: " +
  310. (d != null ? d.toString() : "UNKNOWN"));
  311.  
  312. // FLAGS
  313. Flags flags = m.getFlags();
  314. StringBuffer sb = new StringBuffer();
  315. Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
  316.  
  317. boolean first = true;
  318. for (int i = 0; i < sf.length; i++) {
  319. String s;
  320. Flags.Flag f = sf[i];
  321. if (f == Flags.Flag.ANSWERED)
  322. s = "\\Answered";
  323. else if (f == Flags.Flag.DELETED)
  324. s = "\\Deleted";
  325. else if (f == Flags.Flag.DRAFT)
  326. s = "\\Draft";
  327. else if (f == Flags.Flag.FLAGGED)
  328. s = "\\Flagged";
  329. else if (f == Flags.Flag.RECENT)
  330. s = "\\Recent";
  331. else if (f == Flags.Flag.SEEN)
  332. s = "\\Seen";
  333. else
  334. continue; // skip it
  335. if (first)
  336. first = false;
  337. else
  338. sb.append(' ');
  339. sb.append(s);
  340. }
  341.  
  342. String[] uf = flags.getUserFlags(); // get the user flag strings
  343. for (int i = 0; i < uf.length; i++) {
  344. if (first)
  345. first = false;
  346. else
  347. sb.append(' ');
  348. sb.append(uf[i]);
  349. }
  350. pr("FLAGS: " + sb.toString());
  351.  
  352. // X-MAILER
  353. String[] hdrs = m.getHeader("X-Mailer");
  354. if (hdrs != null)
  355. pr("X-Mailer: " + hdrs[0]);
  356. else
  357. pr("X-Mailer NOT available");
  358. }
  359.  
  360. static String indentStr = " ";
  361. static int level = 0;
  362.  
  363. /**
  364. * Print a, possibly indented, string.
  365. */
  366. public static void pr(String s) {
  367. if (showStructure)
  368. System.out.print(indentStr.substring(0, level * 2));
  369. System.out.println(s);
  370. }
  371. }
Add Comment
Please, Sign In to add comment