javamail接收邮件,设置已读时报错
使用IMAP协议接收邮件,服务器连接,邮件读取都正常,但是运行到messages.setFlag(Flags.Flag.SEEN, true); 这一步时就会报错。错误代码如下:Exception in thread "main" javax.mail.MessagingException: A6 NO STORE State error;
nested exception is:
com.sun.mail.iap.CommandFailedException: A6 NO STORE State error
at com.sun.mail.imap.IMAPMessage.setFlags(IMAPMessage.java:858)
at javax.mail.Message.setFlag(Message.java:574)
at com.MailReceive.main(MailReceive.java:59)
Caused by: com.sun.mail.iap.CommandFailedException: A6 NO STORE State error
at com.sun.mail.iap.Protocol.handleResult(Protocol.java:336)
at com.sun.mail.imap.protocol.IMAPProtocol.storeFlags(IMAPProtocol.java:1373)
at com.sun.mail.imap.protocol.IMAPProtocol.storeFlags(IMAPProtocol.java:1358)
at com.sun.mail.imap.IMAPMessage.setFlags(IMAPMessage.java:854)
... 2 more
希望大家帮忙看下是什么原因。 javamail java --------------------编程问答-------------------- folder.open(Folder.READ_WRITE); 设置错误了吧 你应该是设置成READ_ONLY了 --------------------编程问答-------------------- 没看出来错误原因。我这有个imap写的方法 你参考下吧
package com.email;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
/**
* 使用imap协议获取未读邮件数
* @author abc
*
*/
public class GetEmailImap {
public static void main(String[] args) throws Exception {
String user = "***@163.com";// 邮箱的用户名
String password = "***"; // 邮箱的密码
Properties prop = System.getProperties();
prop.put("mail.store.protocol", "imap");
prop.put("mail.imap.host", "imap.163.com");
Session session = Session.getInstance(prop);
int total = 0;
IMAPStore store = (IMAPStore) session.getStore("imap"); // 使用imap会话机制,连接服务器
store.connect(user, password);
IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX"); // 收件箱
folder.open(Folder.READ_WRITE);
// 获取总邮件数
total = folder.getMessageCount();
System.out.println("-----------------共有邮件:" + total
+ " 封--------------");
// 得到收件箱文件夹信息,获取邮件列表
System.out.println("未读邮件数:" + folder.getUnreadMessageCount());
Message[] messages = folder.getMessages();
int messageNumber = 0;
/*if(messages[total - 1].getSubject().equals("delete")){
System.out.println("delete!!!!");
}*/
for (Message message : messages) {
Flags flags = message.getFlags();
if (flags.contains(Flags.Flag.SEEN)) {
System.out.println("已读邮件");
System.out.println("发送时间:" + message.getSentDate());
System.out.println("主题:" + message.getSubject());
System.out.println("内容:" + message.getContent());
} else {
System.out.println("未读邮件");
System.out.println("发送时间:" + message.getSentDate());
System.out.println("主题:" + message.getSubject());
System.out.println("内容:" + message.getContent());
}
// 每封邮件都有一个MessageNumber,可以通过邮件的MessageNumber在收件箱里面取得该邮件
messageNumber = message.getMessageNumber();
}
Message message = folder.getMessage(messageNumber);
System.out.println(message.getContent() + message.getContentType());
// 释放资源
if (folder != null)
folder.close(true);
if (store != null)
store.close();
}
}
补充:Java , Java相关