一个关于Javamail的问题,24分必给!

wlfjck 2001-03-09 08:46:00
/******************************************************************************

@Author : Huang Yingting
@Date : 2001/02/28

******************************************************************************/

import java.io.*;
import java.util.*;
import java.sql.*;
import java.net.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;


public class imap
{

public static void main(String[] args)
{
DataInputStream conStream=
new DataInputStream (
new BufferedInputStream
( System.in ) );

String ls_ContentType = "";

try
{

Session mailSession = Session.getDefaultInstance(System.getProperties(),null);
mailSession.setDebug(true);

Store mailStore = null;

mailStore = mailSession.getStore("imap");
mailStore.connect( "imap.21cn.com",143,
"cderfv_2","cderfv_2" );

Folder mailInbox = mailStore.getDefaultFolder();
mailInbox = mailInbox.getFolder("INBOX");

mailInbox.open(Folder.READ_WRITE);

int msgnum = mailInbox.getMessageCount();
System.out.println(msgnum);
for(int i=1;i<msgnum+1;i++)
{
Message mailMessage = mailInbox.getMessage(i);
ls_ContentType=mailMessage.getContentType();
System.out.println(ls_ContentType);
}

mailInbox.close(true);
mailStore.close();
}
catch(Exception E)
{
E.printStackTrace();
}

try
{
System.in.read();
}
catch(Exception E)
{
;
}

}

}

为什么运行这段程序,系统有些时候会出现NullPointerException的例外,这段程序究竟错在什么地方,请高手指点!
24分必给,决不食言!!
...全文
217 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
panlang 2001-05-25
  • 打赏
  • 举报
回复
to skyyoung(路人甲) 
你又来啦!我的话没有错吧!
你的EMAIL我收到了,只是还没有......真是不好意思,等我忙完了这几天就回!
bluevacuum 2001-05-25
  • 打赏
  • 举报
回复
请注意你的程序中的代码:
Folder mailInbox = mailStore.getDefaultFolder();
mailInbox = mailInbox.getFolder("INBOX");
另外请检查你的代码在何出出现此意外
wlfjck 2001-03-12
  • 打赏
  • 举报
回复
真的没有人知道了吗
shyguy 2001-03-11
  • 打赏
  • 举报
回复
javamail在notes的pop3服务器上问题多多
有谁能够用javamail成功检测新邮件?
wlfjck 2001-03-11
  • 打赏
  • 举报
回复
希望各位严肃点,回答问题不是开玩笑!
这种弱智的回答就不要乱贴了,你们到底懂不懂Javamail!
wlfjck 2001-03-11
  • 打赏
  • 举报
回复
to luodi:
不对,我不知道你有没有运行这段代码!
其实确切的说应该是从1开始循环的!
希望大家多提宝贵意见!其实真正的问题出在当你要想getContentType时
系统会有时出现NullPointerException!
luodi 2001-03-11
  • 打赏
  • 举报
回复
问题出在这一段吧:
// 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);
}

Tommy Chang 2001-03-11
  • 打赏
  • 举报
回复
21cn支持imap

:)
skyyoung 2001-03-10
  • 打赏
  • 举报
回复
我觉得是下面的问题
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");
}
}

wlfjck 2001-03-10
  • 打赏
  • 举报
回复
希望各位高手多讨论!
skyyoung 2001-03-10
  • 打赏
  • 举报
回复
21cn支不支持imap啊
wlfjck 2001-03-10
  • 打赏
  • 举报
回复
非常感谢大家的人情参与,但是正如Skyyoung所说,其实Javamail的Demo中的msgshow事实上也存在着这样的问题,如果运行msgshow,将账号,口令还是输入21cn的话,也会出现NullPointerException,注意这个问题只是在IMAP邮箱中存在,POP3没有。希望大家多给提示!
fatpig521 2001-03-09
  • 打赏
  • 举报
回复
Session mailSession = Session.getDefaultInstance(System.getProperties(),null);
一定能得到非空的Session ?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧