/*
 * @(#)folderlist.java 1.27 00/08/30
 *
 * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */import java.util.Properties;
import javax.mail.*;/**
 * Demo app that exercises the Message interfaces.
 * List information about folders.
 *
 * @author John Mani
 * @author Bill Shannon
 */public class folderlist {
    static String protocol = null;
    static String host = null;
    static String user = null;
    static String password = null;
    static String url = null;
    static String root = null;
    static String pattern = "%";
    static boolean recursive = false;
    static boolean verbose = false;
    static boolean debug = false;    public static void main(String argv[]) throws Exception {
int optind;
for (optind = 0; optind < argv.length; optind++) {
    if (argv[optind].equals("-T")) {
protocol = argv[++optind];
    } else if (argv[optind].equals("-H")) {
host = argv[++optind];
    } else if (argv[optind].equals("-U")) {
user = argv[++optind];
    } else if (argv[optind].equals("-P")) {
password = argv[++optind];
    } else if (argv[optind].equals("-L")) {
url = argv[++optind];
    } else if (argv[optind].equals("-R")) {
root = argv[++optind];
    } else if (argv[optind].equals("-r")) {
recursive = true;
    } else if (argv[optind].equals("-v")) {
verbose = true;
    } else if (argv[optind].equals("-D")) {
debug = true;
    } else if (argv[optind].equals("--")) {
optind++;
break;
    } else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]");
System.out.println(
"\t[-R root] [-r] [-v] [-D] [pattern]");
System.exit(1);
    } else {
break;
    }
}
if (optind < argv.length)
    pattern = argv[optind]; // Get a Properties object
Properties props = System.getProperties(); // Get a Session object
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug); // Get a Store object
Store store = null;
Folder rf = null;
if (url != null) {
    URLName urln = new URLName(url);
    store = session.getStore(urln);
    store.connect();
} else {
    if (protocol != null)
store = session.getStore(protocol);
    else
store = session.getStore();     // Connect
    if (host != null || user != null || password != null)
store.connect(host, user, password);
    else
store.connect();
} // List namespace
if (root != null)
    rf = store.getFolder(root);
else
    rf = store.getDefaultFolder(); dumpFolder(rf, false, "");
if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
    Folder[] f = rf.list(pattern);
    for (int i = 0; i < f.length; i++)
dumpFolder(f[i], recursive, "    ");
} store.close();
    }    static void dumpFolder(Folder folder, boolean recurse, String tab)
throws Exception {
     System.out.println(tab + "Name:      " + folder.getName());
System.out.println(tab + "Full Name: " + folder.getFullName());
System.out.println(tab + "URL:       " + folder.getURLName()); if (verbose) {
    if (!folder.isSubscribed())
System.out.println(tab + "Not Subscribed");     if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
if (folder.hasNewMessages())
    System.out.println(tab + "Has New Messages");
System.out.println(tab + "Total Messages:  " +
folder.getMessageCount());
System.out.println(tab + "New Messages:    " +
folder.getNewMessageCount());
System.out.println(tab + "Unread Messages: " +
folder.getUnreadMessageCount());
    }
    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
System.out.println(tab + "Is Directory");
} System.out.println(); if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
    if (recurse) {
Folder[] f = folder.list();
for (int i = 0; i < f.length; i++)
    dumpFolder(f[i], recurse, tab + "    ");
    }
}
    }
}

解决方案 »

  1.   

    /*
     * @(#)msgshow.java 1.24 00/10/14
     *
     * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
     *
     * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
     * modify and redistribute this software in source and binary code form,
     * provided that i) this copyright notice and license appear on all copies of
     * the software; and ii) Licensee does not utilize the software in a manner
     * which is disparaging to Sun.
     *
     * This software is provided "AS IS," without a warranty of any kind. ALL
     * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
     * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
     * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
     * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
     * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
     * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
     * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
     * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
     * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
     * POSSIBILITY OF SUCH DAMAGES.
     *
     * This software is not designed or intended for use in on-line control of
     * aircraft, air traffic, aircraft navigation or aircraft communications; or in
     * the design, construction, operation or maintenance of any nuclear
     * facility. Licensee represents and warrants that it will not use or
     * redistribute the Software for such purposes.
     */import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;/*
     * Demo app that exercises the Message interfaces.
     * Show information about and contents of messages.
     *
     * @author John Mani
     * @author Bill Shannon
     */public class msgshow {    static String protocol;
        static String host = null;
        static String user = null;
        static String password = null;
        static String mbox = "INBOX";
        static String url = null;
        static int port = -1;
        static boolean verbose = false;
        static boolean debug = false;
        static boolean showStructure = false;
        static boolean showMessage = false;    public static void main(String argv[]) {
    int msgnum = -1;
    int optind; for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
    protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
    host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
    user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
    password = argv[++optind];
        } else if (argv[optind].equals("-v")) {
    verbose = true;
        } else if (argv[optind].equals("-D")) {
    debug = true;
        } else if (argv[optind].equals("-f")) {
    mbox = argv[++optind];
        } else if (argv[optind].equals("-L")) {
    url = argv[++optind];
        } else if (argv[optind].equals("-p")) {
    port = Integer.parseInt(argv[++optind]);
        } else if (argv[optind].equals("-s")) {
    showStructure = true;
        } else if (argv[optind].equals("-m")) {
    showMessage = true;
        } else if (argv[optind].equals("--")) {
    optind++;
    break;
        } else if (argv[optind].startsWith("-")) {
    System.out.println(
    "Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
    System.out.println(
    "\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s]");
    System.out.println(
    "or     msgshow -m [-v] [-D] [-s] < msg");
    System.exit(1);
        } else {
    break;
        }
    }        try {
        if (optind < argv.length)
             msgnum = Integer.parseInt(argv[optind]);     // Get a Properties object
        Properties props = System.getProperties();     // Get a Session object
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);     if (showMessage) {
    MimeMessage msg = new MimeMessage(session, System.in);
    dumpPart(msg);
    System.exit(0);
        }     // Get a Store object
        Store store = null;
        if (url != null) {
    URLName urln = new URLName(url);
    store = session.getStore(urln);
    store.connect();
        } else {
    if (protocol != null)
        store = session.getStore(protocol);
    else
        store = session.getStore(); // Connect
    if (host != null || user != null || password != null)
        store.connect(host, port, user, password);
    else
        store.connect();
        }
             // Open the Folder     Folder folder = store.getDefaultFolder();
        if (folder == null) {
            System.out.println("No default folder");
            System.exit(1);
        }     folder = folder.getFolder(mbox);
        if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
        }     // try to open read/write and if that fails try read-only
        try {
    folder.open(Folder.READ_WRITE);
        } catch (MessagingException ex) {
    folder.open(Folder.READ_ONLY);
        }
        int totalMessages = folder.getMessageCount();     if (totalMessages == 0) {
    System.out.println("Empty folder");
    folder.close(false);
    store.close();
    System.exit(1);
        }     if (verbose) {
    int newMessages = folder.getNewMessageCount();
    System.out.println("Total messages = " + totalMessages);
    System.out.println("New messages = " + newMessages);
    System.out.println("-------------------------------");
        }     if (msgnum == -1) {
    // Attributes & Flags for all messages ..
    Message[] msgs = folder.getMessages(); // Use a suitable FetchProfile
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.ENVELOPE);
    fp.add(FetchProfile.Item.FLAGS);
    fp.add("X-Mailer");
    folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) {
        System.out.println("--------------------------");
        System.out.println("MESSAGE #" + (i + 1) + ":");
        dumpEnvelope(msgs[i]);
        // dumpPart(msgs[i]);
    }
        } else {
    System.out.println("Getting message number: " + msgnum);
    Message m = null;

    try {
        m = folder.getMessage(msgnum);
        dumpPart(m);
    } catch (IndexOutOfBoundsException iex) {
        System.out.println("Message number out of range");
    }
        }     folder.close(false);
        store.close();
    } catch (Exception ex) {
        System.out.println("Oops, got exception! " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
        }    public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message)
        dumpEnvelope((Message)p); /** Dump input stream ..  InputStream is = p.getInputStream();
    // If "is" is not already buffered, wrap a BufferedInputStream
    // around it.
    if (!(is instanceof BufferedInputStream))
        is = new BufferedInputStream(is);
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c); **/ pr("CONTENT-TYPE: " + p.getContentType());
    String filename = p.getFileName();
    if (filename != null)
        pr("FILENAME: " + filename); /*
     * Using isMimeType to determine the content type avoids
     * fetching the actual content data until we need it.
     */
    if (p.isMimeType("text/plain")) {
        pr("This is plain text");
        pr("---------------------------");
        if (!showStructure)
    System.out.println((String)p.getContent());
    } else if (p.isMimeType("multipart/*")) {
        pr("This is a Multipart");
        pr("---------------------------");
        Multipart mp = (Multipart)p.getContent();
        level++;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
    dumpPart(mp.getBodyPart(i));
        level--;
    } else if (p.isMimeType("message/rfc822")) {
        pr("This is a Nested Message");
        pr("---------------------------");
        level++;
        dumpPart((Part)p.getContent());
        level--;
    } else if (!showStructure) {
        /*
         * If we actually want to see the data, and it's not a
         * MIME type we know, fetch it and check its Java type.
         */
        Object o = p.getContent();
        if (o instanceof String) {
    pr("This is a string");
    pr("---------------------------");
    System.out.println((String)o);
        } else if (o instanceof InputStream) {
    pr("This is just an input stream");
    pr("---------------------------");
    InputStream is = (InputStream)o;
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c);
        } else {
    pr("This is an unknown type");
    pr("---------------------------");
    pr(o.toString());
        }
    } else {
        pr("This is an unknown type");
        pr("---------------------------");
    }
        }    public static void dumpEnvelope(Message m) throws Exception {
    pr("This is the message envelope");
    pr("---------------------------");
    Address[] a;
    // FROM 
    if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++)
    pr("FROM: " + a[j].toString());
    } // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++)
    pr("TO: " + a[j].toString());
    } // SUBJECT
    pr("SUBJECT: " + m.getSubject()); // DATE
    Date d = m.getSentDate();
    pr("SendDate: " +
        (d != null ? d.toString() : "UNKNOWN")); // FLAGS
    Flags flags = m.getFlags();
    StringBuffer sb = new StringBuffer();
    Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags boolean first = true;
    for (int i = 0; i < sf.length; i++) {
        String s;
        Flags.Flag f = sf[i];
        if (f == Flags.Flag.ANSWERED)
    s = "\\Answered";
        else if (f == Flags.Flag.DELETED)
    s = "\\Deleted";
        else if (f == Flags.Flag.DRAFT)
    s = "\\Draft";
        else if (f == Flags.Flag.FLAGGED)
    s = "\\Flagged";
        else if (f == Flags.Flag.RECENT)
    s = "\\Recent";
        else if (f == Flags.Flag.SEEN)
    s = "\\Seen";
        else
    continue; // skip it
        if (first)
    first = false;
        else
    sb.append(' ');
        sb.append(s);
    } String[] uf = flags.getUserFlags(); // get the user flag strings
    for (int i = 0; i < uf.length; i++) {
        if (first)
    first = false;
        else
    sb.append(' ');
        sb.append(uf[i]);
    }
    pr("FLAGS: " + sb.toString()); // X-MAILER
    String[] hdrs = m.getHeader("X-Mailer");
    if (hdrs != null)
        pr("X-Mailer: " + hdrs[0]);
    else
        pr("X-Mailer NOT available");
        }    static String indentStr = "                                               ";
        static int level = 0;    /**
         * Print a, possibly indented, string.
         */
        public static void pr(String s) {
    if (showStructure)
        System.out.print(indentStr.substring(0, level * 2));
    System.out.println(s);
        }
    }
      

  2.   

    除非你用imap协议,否则用pop3协议是不可能收到那些邮件的
      

  3.   

    wujj你能发一个收收件箱的完整例子给我吗?我现在用servlet收邮件老不行,[email protected]
      

  4.   

    我是用jsp写的
    还有很乱码问题的
      

  5.   

    500 Servlet Exception
    javax.mail.NoSuchProviderException: No provider for iamp
    at javax.mail.Session.getProvider(Session.java:289)
    at javax.mail.Session.getStore(Session.java:363)
    at javax.mail.Session.getStore(Session.java:343)
    at _webmail._numshow__jsp._jspService(/webmail/numshow.jsp:18)
    at com.caucho.jsp.JavaPage.service(JavaPage.java:87)
    at com.caucho.jsp.JavaPage.subservice(JavaPage.java:81)
    at com.caucho.jsp.Page.service(Page.java:407)
    at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:166)
    at com.caucho.server.http.Invocation.service(Invocation.java:273)
    at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:128)
    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:216)
    at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:158)
    at com.caucho.server.TcpConnection.run(TcpConnection.java:140)
    at java.lang.Thread.run(Thread.java:484)
      

  6.   

    Usually this is because JavaMail can't access the configuration files in mail.jar, possibly because of a security permission problem; see this item for more details. Also, make sure that you haven't extracted the mail.jar contents; you should include the unmodified mail.jar file in the server's CLASSPATH. 
      

  7.   

    如果服务器不支持imap协议的话,那么这些信件是没有办法收下来的。你只能网页访问这个邮箱
    把你想要的新建移动到收件箱才行