关于JavaMail的问题,想请教大家,希望多回复!困扰好几天了! ( 积分: 50 )

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