我觉得是下面的问题 mailStore = mailSession.getStore("imap"); mailStore.connect( "imap.21cn.com",143,"cderfv_2","cderfv_2" ); =============================================================== 下面是javamail包里的example,可以在linux以命令行参数来show message,这样测试比较方便.另外,21cn是否支持imap协议访问。 /* * @(#)msgshow.java 1.18 98/10/28 * * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. * */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; 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("--")) { 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]"); 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); // 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); } folder.open(Folder.READ_WRITE); 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); } 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. Note that the default buffer size of 510 bytes is // probably too low ... if (!(is instanceof BufferedInputStream)) is = new BufferedInputStream(p.getInputStream()); int c; while ((c = is.read()) != -1) System.out.write(c); **/ System.out.println("CONTENT-TYPE: " + p.getContentType()); Object o = p.getContent(); if (o instanceof String) { System.out.println("This is a String"); System.out.println("---------------------------"); System.out.println((String)o); } else if (o instanceof Multipart) { System.out.println("This is a Multipart"); System.out.println("---------------------------"); Multipart mp = (Multipart)o; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); } else if (o instanceof Message) { System.out.println("This is a Nested Message"); System.out.println("---------------------------"); dumpPart((Part)o); } else if (o instanceof InputStream) { System.out.println("This is just an input stream"); System.out.println("---------------------------"); InputStream is = (InputStream)o; int c; while ((c = is.read()) != -1) System.out.write(c); } } public static void dumpEnvelope(Message m) throws Exception { System.out.println("This is the message envelope"); System.out.println("---------------------------"); Address[] a; // FROM if ((a = m.getFrom()) != null) { for (int j = 0; j < a.length; j++) System.out.println("FROM: " + a[j].toString()); } // TO if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { for (int j = 0; j < a.length; j++) System.out.println("TO: " + a[j].toString()); } // SUBJECT System.out.println("SUBJECT: " + m.getSubject()); // DATE Date d = m.getSentDate(); System.out.println("SendDate: " + (d != null ? d.toString() : "UNKNOWN")); // SIZE System.out.println("Size: " + m.getSize()); // 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]); } System.out.println("FLAGS = " + sb.toString()); // X-MAILER String[] hdrs = m.getHeader("X-Mailer"); if (hdrs != null) System.out.println("X-Mailer: " + hdrs[0]); else System.out.println("X-Mailer NOT available"); } }
mailStore = mailSession.getStore("imap");
mailStore.connect( "imap.21cn.com",143,"cderfv_2","cderfv_2" );
===============================================================
下面是javamail包里的example,可以在linux以命令行参数来show message,这样测试比较方便.另外,21cn是否支持imap协议访问。
/*
* @(#)msgshow.java 1.18 98/10/28
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/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; 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("--")) {
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]");
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); // 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);
} folder.open(Folder.READ_WRITE);
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);
} 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. Note that the default buffer size of 510 bytes is
// probably too low ...
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(p.getInputStream());
int c;
while ((c = is.read()) != -1)
System.out.write(c); **/ System.out.println("CONTENT-TYPE: " + p.getContentType()); Object o = p.getContent();
if (o instanceof String) {
System.out.println("This is a String");
System.out.println("---------------------------");
System.out.println((String)o);
} else if (o instanceof Multipart) {
System.out.println("This is a Multipart");
System.out.println("---------------------------");
Multipart mp = (Multipart)o;
int count = mp.getCount();
for (int i = 0; i < count; i++)
dumpPart(mp.getBodyPart(i));
} else if (o instanceof Message) {
System.out.println("This is a Nested Message");
System.out.println("---------------------------");
dumpPart((Part)o);
} else if (o instanceof InputStream) {
System.out.println("This is just an input stream");
System.out.println("---------------------------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
}
} public static void dumpEnvelope(Message m) throws Exception {
System.out.println("This is the message envelope");
System.out.println("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("FROM: " + a[j].toString());
} // TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("TO: " + a[j].toString());
} // SUBJECT
System.out.println("SUBJECT: " + m.getSubject()); // DATE
Date d = m.getSentDate();
System.out.println("SendDate: " +
(d != null ? d.toString() : "UNKNOWN")); // SIZE
System.out.println("Size: " + m.getSize()); // 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]);
}
System.out.println("FLAGS = " + sb.toString()); // X-MAILER
String[] hdrs = m.getHeader("X-Mailer");
if (hdrs != null)
System.out.println("X-Mailer: " + hdrs[0]);
else
System.out.println("X-Mailer NOT available");
}
}
这种弱智的回答就不要乱贴了,你们到底懂不懂Javamail!
有谁能够用javamail成功检测新邮件?
// for(int i=1;i<msgnum+1;i++) ==>
for(int i=0; i < msgnum; i++)
{
Message mailMessage = mailInbox.getMessage(i);
ls_ContentType=mailMessage.getContentType();
System.out.println(ls_ContentType);
}
不对,我不知道你有没有运行这段代码!
其实确切的说应该是从1开始循环的!
希望大家多提宝贵意见!其实真正的问题出在当你要想getContentType时
系统会有时出现NullPointerException!
Folder mailInbox = mailStore.getDefaultFolder();
mailInbox = mailInbox.getFolder("INBOX");
另外请检查你的代码在何出出现此意外
你又来啦!我的话没有错吧!
你的EMAIL我收到了,只是还没有......真是不好意思,等我忙完了这几天就回!