H
h_o_u_l_i
Unregistered / Unconfirmed
GUEST, unregistred user!
我的JavaMail程序,接收(用POP3协议)及发送(用SMTP)邮件都可以,但就是判断是否是未读邮件的时候
总是认为是未读的,即使是已经打开并阅读过的邮件也是如此,这是为什么?????
下面是判断是否是未读邮件的代码:
(1)获得邮箱中所有邮件的列表清单:
public Vector getInboxDetailMessage() throws Exception {
v_mailDetailMessage=new Vector();
try {
this.store = mailsession.getStore("pop3"
this.store.connect(hostname, this.username, this.password);
this.folder_inbox = store.getFolder("inbox"
this.folder_inbox.open(Folder.READ_ONLY);
Message message = null;
Address address[] = null;
DateFormat d = DateFormat.getDateTimeInstance();
for (int i = 1;
i <= folder_inbox.getMessageCount();
i++) {
HashMap hashmap = new HashMap();
message = folder_inbox.getMessage(i);
//获取发件人地址
address = message.getFrom();
String strAddress = "";
for (int j = 0;
j < address.length;
j++) {
strAddress = address[j].toString() + "
";
}
hashmap.put("From", strAddress);
//获取邮件标题
hashmap.put("subject", message.getSubject());
//获取发件日期
hashmap.put("sendDate", d.format(message.getSentDate()));
//存储IsUnRead标记(Key)表示是否此邮件已读
if (!message.isSet(Flags.Flag.SEEN)) {
hashmap.put("IsUnRead", "yes"//?????总是执行此句而从不运行else
后的语句????
} else
{
hashmap.put("IsUnRead", "no"
}
//存储邮件在邮件夹中的序号
hashmap.put("Num", Integer.toString(i));
v_mailDetailMessage.add(hashmap);
}
} catch (Authen
ticationFailedException e) {
throw new Authen
ticationFailedException("用户名或密码输入错误!"
} catch (MessagingException e) {
throw new MessagingException("连接Store(连接邮件箱时错误!)."
+ "/n"
+
"exception.getMessage():"
+
e.getMessage());
}finally{
this.folder_inbox.close(true);
store.close();
}
return v_mailDetailMessage;
}
(2)获取邮箱基本信息,即未读邮件数,总共邮件数
public HashMap getInboxBaseMessage() throws Exception {
HashMap hashmap = new HashMap();
try {
this.store = mailsession.getStore("pop3"
this.store.connect(hostname, this.username, this.password);
this.folder_inbox = store.getFolder("inbox"
this.folder_inbox.open(Folder.READ_ONLY);
//????未读邮件数,总是返回所有的邮件都是未读的,尽管有的已经阅读过????
hashmap.put("UnreadMessageCount",
Integer.toString(this.folder_inbox.
getUnreadMessageCount()));
//共有邮件数
hashmap.put("MessageCount",
Integer.toString(this.folder_inbox.getMessageCount()));
} catch (Authen
ticationFailedException e) {
throw new Authen
ticationFailedException("用户名或密码输入错误!"
} catch (MessagingException e) {
throw new MessagingException("连接Store(连接邮件箱时错误!)."
+ "/n"
+
"exception.getMessage():"
+
e.getMessage());
}finally{
this.folder_inbox.close(true);
store.close();
}
return hashmap;
}
上面“?”处代码返回总是有问题,请指教呀!!!先谢了!
另外,在浏览邮件内容时用message.setFlag(Flags.Flag.SEEN,true);进行设置,下次浏览时仍显示为“未读“。
总是认为是未读的,即使是已经打开并阅读过的邮件也是如此,这是为什么?????
下面是判断是否是未读邮件的代码:
(1)获得邮箱中所有邮件的列表清单:
public Vector getInboxDetailMessage() throws Exception {
v_mailDetailMessage=new Vector();
try {
this.store = mailsession.getStore("pop3"
this.store.connect(hostname, this.username, this.password);
this.folder_inbox = store.getFolder("inbox"
this.folder_inbox.open(Folder.READ_ONLY);
Message message = null;
Address address[] = null;
DateFormat d = DateFormat.getDateTimeInstance();
for (int i = 1;
i <= folder_inbox.getMessageCount();
i++) {
HashMap hashmap = new HashMap();
message = folder_inbox.getMessage(i);
//获取发件人地址
address = message.getFrom();
String strAddress = "";
for (int j = 0;
j < address.length;
j++) {
strAddress = address[j].toString() + "
";
}
hashmap.put("From", strAddress);
//获取邮件标题
hashmap.put("subject", message.getSubject());
//获取发件日期
hashmap.put("sendDate", d.format(message.getSentDate()));
//存储IsUnRead标记(Key)表示是否此邮件已读
if (!message.isSet(Flags.Flag.SEEN)) {
hashmap.put("IsUnRead", "yes"//?????总是执行此句而从不运行else
后的语句????
} else
{
hashmap.put("IsUnRead", "no"
}
//存储邮件在邮件夹中的序号
hashmap.put("Num", Integer.toString(i));
v_mailDetailMessage.add(hashmap);
}
} catch (Authen
ticationFailedException e) {
throw new Authen
ticationFailedException("用户名或密码输入错误!"
} catch (MessagingException e) {
throw new MessagingException("连接Store(连接邮件箱时错误!)."
+ "/n"
+
"exception.getMessage():"
+
e.getMessage());
}finally{
this.folder_inbox.close(true);
store.close();
}
return v_mailDetailMessage;
}
(2)获取邮箱基本信息,即未读邮件数,总共邮件数
public HashMap getInboxBaseMessage() throws Exception {
HashMap hashmap = new HashMap();
try {
this.store = mailsession.getStore("pop3"
this.store.connect(hostname, this.username, this.password);
this.folder_inbox = store.getFolder("inbox"
this.folder_inbox.open(Folder.READ_ONLY);
//????未读邮件数,总是返回所有的邮件都是未读的,尽管有的已经阅读过????
hashmap.put("UnreadMessageCount",
Integer.toString(this.folder_inbox.
getUnreadMessageCount()));
//共有邮件数
hashmap.put("MessageCount",
Integer.toString(this.folder_inbox.getMessageCount()));
} catch (Authen
ticationFailedException e) {
throw new Authen
ticationFailedException("用户名或密码输入错误!"
} catch (MessagingException e) {
throw new MessagingException("连接Store(连接邮件箱时错误!)."
+ "/n"
+
"exception.getMessage():"
+
e.getMessage());
}finally{
this.folder_inbox.close(true);
store.close();
}
return hashmap;
}
上面“?”处代码返回总是有问题,请指教呀!!!先谢了!
另外,在浏览邮件内容时用message.setFlag(Flags.Flag.SEEN,true);进行设置,下次浏览时仍显示为“未读“。