Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36. import javax.activation.*;
  37.  
  38. /*
  39.  * Demo app that exercises the Message interfaces.
  40.  * Access message given its UID.
  41.  * Show information about and contents of messages.
  42.  *
  43.  * @author John Mani
  44.  * @author Bill Shannon
  45.  */
  46.  
  47. public class uidmsgshow {
  48.  
  49.     static String protocol;
  50.     static String host = null;
  51.     static String user = null;
  52.     static String password = null;
  53.     static String mbox = "INBOX";
  54.     static String url = null;
  55.     static boolean verbose = false;
  56.  
  57.     public static void main(String argv[]) {
  58.         long uid = -1;
  59.         int optind;
  60.  
  61.         for (optind = 0; optind < argv.length; optind++) {
  62.             if (argv[optind].equals("-T")) {
  63.                 protocol = argv[++optind];
  64.             } else if (argv[optind].equals("-H")) {
  65.                 host = argv[++optind];
  66.             } else if (argv[optind].equals("-U")) {
  67.                 user = argv[++optind];
  68.             } else if (argv[optind].equals("-P")) {
  69.                 password = argv[++optind];
  70.             } else if (argv[optind].equals("-v")) {
  71.                 verbose = true;
  72.             } else if (argv[optind].equals("-f")) {
  73.                 mbox = argv[++optind];
  74.             } else if (argv[optind].equals("-L")) {
  75.                 url = argv[++optind];
  76.             } else if (argv[optind].equals("--")) {
  77.                 optind++;
  78.                 break;
  79.             } else if (argv[optind].startsWith("-")) {
  80.                 System.out.println("Usage: uidmsgshow [-L url] [-T protocol] [-H host] [-U user] [-P password] [-f mailbox] [uid] [-v]");
  81.                 System.exit(1);
  82.             } else {
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         try {
  88.             if (optind < argv.length)
  89.                  uid = Long.parseLong(argv[optind]);
  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(true);
  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("No default folder");
  123.                 System.exit(1);
  124.             }
  125.  
  126.             folder = folder.getFolder(mbox);
  127.             if (!folder.exists()) {
  128.                 System.out.println(mbox + "  does not exist");
  129.                 System.exit(1);
  130.             }
  131.  
  132.             if (!(folder instanceof UIDFolder)) {
  133.                 System.out.println(
  134.                     "This Provider or this folder does not support UIDs");
  135.                 System.exit(1);
  136.             }
  137.  
  138.             UIDFolder ufolder = (UIDFolder)folder;
  139.  
  140.             folder.open(Folder.READ_WRITE);
  141.             int totalMessages = folder.getMessageCount();
  142.  
  143.             if (totalMessages == 0) {
  144.                 System.out.println("Empty folder");
  145.                 folder.close(false);
  146.                 store.close();
  147.                 System.exit(1);
  148.             }
  149.  
  150.             if (verbose) {
  151.                 int newMessages = folder.getNewMessageCount();
  152.                 System.out.println("Total messages = " + totalMessages);
  153.                 System.out.println("New messages = " + newMessages);
  154.                 System.out.println("-------------------------------");
  155.             }
  156.  
  157.             if (uid == -1) {
  158.                 // Attributes & Flags for ALL messages ..
  159.                 Message[] msgs =
  160.                         ufolder.getMessagesByUID(1, UIDFolder.LASTUID);
  161.  
  162.                 // Use a suitable FetchProfile
  163.                 FetchProfile fp = new FetchProfile();
  164.                 fp.add(FetchProfile.Item.ENVELOPE);
  165.                 fp.add(FetchProfile.Item.FLAGS);
  166.                 fp.add("X-Mailer");
  167.                 folder.fetch(msgs, fp);
  168.  
  169.                 for (int i = 0; i < msgs.length; i++) {
  170.                     System.out.println("--------------------------");
  171.                     System.out.println("MESSAGE UID #" +
  172.                                         ufolder.getUID(msgs[i]) + ":");
  173.                     dumpEnvelope(msgs[i]);
  174.                     // dumpPart(msgs[i]);
  175.                 }
  176.             } else {
  177.                 System.out.println("Getting message UID: " + uid);
  178.                 Message m = ufolder.getMessageByUID(uid);
  179.                 if (m != null)
  180.                     dumpPart(m);
  181.                 else
  182.                     System.out.println(
  183.                         "This Message does not exist on this folder");
  184.             }
  185.  
  186.             folder.close(false);
  187.             store.close();
  188.         } catch (Exception ex) {
  189.             System.out.println("Oops, got exception! " + ex.getMessage());
  190.             ex.printStackTrace();
  191.         }
  192.         System.exit(1);
  193.     }
  194.  
  195.     public static void dumpPart(Part p) throws Exception {
  196.         if (p instanceof Message)
  197.             dumpEnvelope((Message)p);
  198.  
  199.         /* Dump input stream
  200.         InputStream is = new BufferedInputStream(p.getInputStream());
  201.         int c;
  202.         while ((c = is.read()) != -1)
  203.             System.out.write(c);
  204.         */
  205.  
  206.         System.out.println("CONTENT-TYPE: " + p.getContentType());
  207.  
  208.         Object o = p.getContent();
  209.         if (o instanceof String) {
  210.             System.out.println("This is a String");
  211.             System.out.println("---------------------------");
  212.             System.out.println((String)o);
  213.         } else if (o instanceof Multipart) {
  214.             System.out.println("This is a Multipart");
  215.             System.out.println("---------------------------");
  216.             Multipart mp = (Multipart)o;
  217.             int count = mp.getCount();
  218.             for (int i = 0; i < count; i++)
  219.                 dumpPart(mp.getBodyPart(i));
  220.         } else if (o instanceof Message) {
  221.             System.out.println("This is a Nested Message");
  222.             System.out.println("---------------------------");
  223.             dumpPart((Part)o);
  224.         } else if (o instanceof InputStream) {
  225.             System.out.println("This is just an input stream");
  226.             System.out.println("---------------------------");
  227.             InputStream is = (InputStream)o;
  228.             int c;
  229.             while ((c = is.read()) != -1)
  230.                 System.out.write(c);
  231.         }
  232.     }
  233.  
  234.     public static void dumpEnvelope(Message m) throws Exception {
  235.         System.out.println("This is the message envelope");
  236.         System.out.println("---------------------------");
  237.         Address[] a;
  238.         // FROM
  239.         if ((a = m.getFrom()) != null) {
  240.             for (int j = 0; j < a.length; j++)
  241.                 System.out.println("FROM: " + a[j].toString());
  242.         }
  243.  
  244.         // TO
  245.         if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  246.             for (int j = 0; j < a.length; j++)
  247.                 System.out.println("TO: " + a[j].toString());
  248.         }
  249.  
  250.         // SUBJECT
  251.         System.out.println("SUBJECT: " + m.getSubject());
  252.  
  253.         // DATE
  254.         Date d = m.getSentDate();
  255.         System.out.println("SendDate: " +
  256.             (d != null ? d.toString() : "UNKNOWN"));
  257.  
  258.         // SIZE
  259.         System.out.println("Size: " + m.getSize());
  260.  
  261.         // FLAGS:
  262.         Flags flags = m.getFlags();
  263.         StringBuffer sb = new StringBuffer();
  264.         Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
  265.  
  266.         boolean first = true;
  267.         for (int i = 0; i < sf.length; i++) {
  268.             String s;
  269.             Flags.Flag f = sf[i];
  270.             if (f == Flags.Flag.ANSWERED)
  271.                 s = "\\Answered";
  272.             else if (f == Flags.Flag.DELETED)
  273.                 s = "\\Deleted";
  274.             else if (f == Flags.Flag.DRAFT)
  275.                 s = "\\Draft";
  276.             else if (f == Flags.Flag.FLAGGED)
  277.                 s = "\\Flagged";
  278.             else if (f == Flags.Flag.RECENT)
  279.                 s = "\\Recent";
  280.             else if (f == Flags.Flag.SEEN)
  281.                 s = "\\Seen";
  282.             else
  283.                 continue;       // skip it
  284.             if (first)
  285.                 first = false;
  286.             else
  287.                 sb.append(' ');
  288.             sb.append(s);
  289.         }
  290.  
  291.         String[] uf = flags.getUserFlags(); // get the user flag strings
  292.         for (int i = 0; i < uf.length; i++) {
  293.             if (first)
  294.                 first = false;
  295.             else
  296.                 sb.append(' ');
  297.             sb.append(uf[i]);
  298.         }
  299.         System.out.println("FLAGS = " + sb.toString());
  300.  
  301.         // X-MAILER
  302.         String[] hdrs = m.getHeader("X-Mailer");
  303.         if (hdrs != null)
  304.             System.out.println("X-Mailer: " + hdrs[0]);
  305.         else
  306.             System.out.println("X-Mailer NOT available");
  307.     }
  308. }
  309.