用JavaMail通过IMAP协议接收qq邮箱时出现“A0 BAD 命令无效或者不支持”的解决方法
2011-01-24 12:47
579 查看
最开始的java代码如下
该程序连接163邮箱时是正常的,但连接qq邮箱时会出错。
调用session.setDebug(true);后发现连qq邮箱的debug信息如下
上Google搜“java mail imap qq 邮箱”,发现《JavaMail中接收邮件的问题
》里提到,需要设置mail.imap.auth.plain.disable为true,但设置完后仍会出错。
继续搜mail.imap.auth.plain.disable,在api文档
中发现另外一个属性mail.imap.auth.login.disable,文档中提到“If true, prevents use of the non-standard
command, instead using the plain
command. Default is false.”,而根据debug信息,连接qq邮箱也是在A0 AUTHENTICATE LOGIN之后提示错误,所以明显是这个参数的问题。
设置参数mail.imap.auth.login.disable为true后连接qq邮箱正常,最终程序如下
正确的debug信息如下
import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Folder; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import ce.mail.models.ConnectionProfile; public class ImapProtocolImpl extends Authenticator implements Protocol { private Session session; private PasswordAuthentication authentication; public ImapProtocolImpl(ConnectionProfile profile, String username, String password) { Properties props = new Properties(); props.setProperty("mail.store.protocol", profile.getProtocol()); props.setProperty("mail.imap.host", profile.getFetchServer()); props.setProperty("mail.imap.port", profile.getFetchPort()); authentication = new PasswordAuthentication(username, password); session = Session.getInstance(props, this); } @Override public PasswordAuthentication getPasswordAuthentication() { return this.authentication; } public void connect() { try { Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); System.out.println(inbox.getMessageCount()); } catch (MessagingException e) { try { byte[] buf = e.getMessage().getBytes("ISO-8859-1"); System.out.println(new String(buf, "GBK")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } throw new RuntimeException("登录失败", e); } } }
该程序连接163邮箱时是正常的,但连接qq邮箱时会出错。
调用session.setDebug(true);后发现连qq邮箱的debug信息如下
DEBUG: setDebug: JavaMail version 1.4.3 DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc] DEBUG: mail.imap.fetchsize: 16384 DEBUG: mail.imap.statuscachetimeout: 1000 DEBUG: mail.imap.appendbuffersize: -1 DEBUG: mail.imap.minidletime: 10 DEBUG: trying to connect to host "imap.qq.com", port 143, isSSL false * OK [CAPABILITY IMAP4 IMAP4rev1 AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready IMAP DEBUG: AUTH: LOGIN DEBUG: protocolConnect login, host=imap.qq.com, user=<qq号码>, password=<non-null> A0 AUTHENTICATE LOGIN A0 BAD ������Ч���߲�֧�� A0 BAD 命令无效或者不支持
上Google搜“java mail imap qq 邮箱”,发现《JavaMail中接收邮件的问题
》里提到,需要设置mail.imap.auth.plain.disable为true,但设置完后仍会出错。
继续搜mail.imap.auth.plain.disable,在api文档
中发现另外一个属性mail.imap.auth.login.disable,文档中提到“If true, prevents use of the non-standard
AUTHENTICATE LOGIN
command, instead using the plain
LOGIN
command. Default is false.”,而根据debug信息,连接qq邮箱也是在A0 AUTHENTICATE LOGIN之后提示错误,所以明显是这个参数的问题。
设置参数mail.imap.auth.login.disable为true后连接qq邮箱正常,最终程序如下
import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Folder; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import ce.mail.models.ConnectionProfile; public class ImapProtocolImpl extends Authenticator implements Protocol { private Session session; private PasswordAuthentication authentication; public ImapProtocolImpl(ConnectionProfile profile, String username, String password) { Properties props = new Properties(); props.setProperty("mail.store.protocol", profile.getProtocol()); props.setProperty("mail.imap.host", profile.getFetchServer()); props.setProperty("mail.imap.port", profile.getFetchPort()); props.setProperty("mail.imap.auth.login.disable", "true"); authentication = new PasswordAuthentication(username, password); session = Session.getInstance(props, this); session.setDebug(true); } @Override public PasswordAuthentication getPasswordAuthentication() { return this.authentication; } public void connect() { try { Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); System.out.println(inbox.getMessageCount()); } catch (MessagingException e) { try { byte[] buf = e.getMessage().getBytes("ISO-8859-1"); System.out.println(new String(buf, "GBK")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } throw new RuntimeException("登录失败", e); } } }
正确的debug信息如下
DEBUG: setDebug: JavaMail version 1.4.3 DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc] DEBUG: mail.imap.fetchsize: 16384 DEBUG: mail.imap.statuscachetimeout: 1000 DEBUG: mail.imap.appendbuffersize: -1 DEBUG: mail.imap.minidletime: 10 DEBUG: disable AUTH=LOGIN DEBUG: trying to connect to host "imap.qq.com", port 143, isSSL false * OK [CAPABILITY IMAP4 IMAP4rev1 AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready IMAP DEBUG: AUTH: LOGIN DEBUG: protocolConnect login, host=imap.qq.com, user=<qq号码>, password=<non-null> A0 LOGIN <qq号码> <未加密的密码> A0 OK Success login ok A1 CAPABILITY * CAPABILITY IMAP4 IMAP4rev1 NAMESPACE CHILDREN A1 OK CAPABILITY Completed DEBUG: connection available -- size: 1 A2 SELECT inbox * 26 EXISTS * 0 RECENT * OK [UNSEEN 1] * OK [UIDVALIDITY 1295833210] UID validity status * OK [UIDNEXT 29] Predicted next UID * FLAGS (/Answered /Flagged /Deleted /Draft /Seen) * OK [PERMANENTFLAGS (/* /Answered /Flagged /Deleted /Draft /Seen)] Permanent flags A2 OK [READ-WRITE] SELECT complete 26
相关文章推荐
- sqlserver2008查看表记录或者修改存储过程出现目录名无效错误解决方法
- rtx出现无法接收图片或者消息库被占用的问题的解决方法
- Android使用wifi通过UDP协议发送广播数据包给PC接收不到问题解决方法
- bash 中 while读取文件并通过 ssh执行命令出现的问题及解决方法
- sqlserver2008查看表记录或者修改存储过程出现目录名无效错误解决方法
- 在win 8.1中安装jdk-7u51 版本中出现javac命令无效的解决方法
- 新建或者导入项目出现"building gradle project info"一直卡住的解决方法
- ubuntu+nginx 出现502 bad gateway解决方法
- 安装交叉编译工具,执行arm-linux-gcc –v命令出现提示找不到该文件或目录?解决方法
- Eclipse Maven Build命令执行无效解决方法
- Linq查询出现"此上下文仅支持基元类型或枚举类型。"解决方法
- VS2010 F5调试时出现:“ 尝试运行项目时出错:未捕获通过反射调用的方法引发的异常”解决
- NSURLSession解决下载中的进度跟进问题——通过实现NSURLSessionDownloadDelegate协议中的回调方法
- 系统恢复后,通过sqlserver服务管理器启动出现“指定的服务未安装”的解决方法
- 接收到开始本地截图的命令:videoshot.exe 解决方法
- Centos7 Minni 安装 执行ifconfig命令出现 -bash ifconfig command not found 的解决方法
- apt update 出现签名错误(无效)的解决方法
- 解决SQL Server 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误或者需要重启N次后还提示需要重启的方法
- 解决无法停用网卡,出现“此时无法停用连接。这个连接可能在用一个或多个不支持即插即用的协议”错误提示