首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

用JavaMail通过转:MAP协议接收qq邮箱时出现“A0 BAD 命令无效或者不支持”的解决办法

2012-12-19 
用JavaMail通过转:MAP协议接收qq邮箱时出现“A0 BAD 命令无效或者不支持”的解决方法转:http://blog.csdn.ne

用JavaMail通过转:MAP协议接收qq邮箱时出现“A0 BAD 命令无效或者不支持”的解决方法

转:http://blog.csdn.net/snowclash/archive/2011/01/24/6160999.aspx

?

最开始的java代码如下

?

view plaincopy to clipboardprint?
  1. import?java.io.UnsupportedEncodingException;??
  2. import?java.util.Properties;??
  3. import?javax.mail.Authenticator;??
  4. import?javax.mail.Folder;??
  5. import?javax.mail.MessagingException;??
  6. import?javax.mail.PasswordAuthentication;??
  7. import?javax.mail.Session;??
  8. import?javax.mail.Store;??
  9. import?ce.mail.models.ConnectionProfile;??
  10. public?class?ImapProtocolImpl?extends?Authenticator?implements?Protocol??
  11. {??
  12. ????private?Session?session;??
  13. ????private?PasswordAuthentication?authentication;??
  14. ??????
  15. ????public?ImapProtocolImpl(ConnectionProfile?profile,?String?username,?String?password)??
  16. ????{??
  17. ????????Properties?props?=?new?Properties();??
  18. ????????props.setProperty("mail.store.protocol",?profile.getProtocol());??
  19. ????????props.setProperty("mail.imap.host",?profile.getFetchServer());??
  20. ????????props.setProperty("mail.imap.port",?profile.getFetchPort());??
  21. ??????????
  22. ????????authentication?=?new?PasswordAuthentication(username,?password);??
  23. ????????session?=?Session.getInstance(props,?this);??
  24. ????}??
  25. ??????
  26. ????@Override??
  27. ????public?PasswordAuthentication?getPasswordAuthentication()??
  28. ????{??
  29. ????????return?this.authentication;??
  30. ????}??
  31. ??????
  32. ????public?void?connect()??
  33. ????{??
  34. ????????try??
  35. ????????{??
  36. ????????????Store?store?=?session.getStore();??
  37. ????????????store.connect();??
  38. ????????????Folder?root?=?store.getDefaultFolder();??
  39. ????????????Folder?inbox?=?root.getFolder("inbox");??
  40. ????????????inbox.open(Folder.READ_WRITE);??
  41. ????????????System.out.println(inbox.getMessageCount());??
  42. ????????}??
  43. ????????catch?(MessagingException?e)??
  44. ????????{??
  45. ????????????try??
  46. ????????????{??
  47. ????????????????byte[]?buf?=?e.getMessage().getBytes("ISO-8859-1");??
  48. ????????????????System.out.println(new?String(buf,?"GBK"));??
  49. ????????????}??
  50. ????????????catch?(UnsupportedEncodingException?e1)??
  51. ????????????{??
  52. ????????????????e1.printStackTrace();??
  53. ????????????}??
  54. ????????????throw?new?RuntimeException("登录失败",?e);??
  55. ????????}??
  56. ????}??
  57. }??

?

该程序连接163邮箱时是正常的,但连接qq邮箱时会出错。

调用session.setDebug(true);后发现连qq邮箱的debug信息如下

?

view plaincopy to clipboardprint?
  1. DEBUG:?setDebug:?JavaMail?version?1.4.3??
  2. DEBUG:?getProvider()?returning?javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun?Microsystems,?Inc]??
  3. DEBUG:?mail.imap.fetchsize:?16384??
  4. DEBUG:?mail.imap.statuscachetimeout:?1000??
  5. DEBUG:?mail.imap.appendbuffersize:?-1??
  6. DEBUG:?mail.imap.minidletime:?10??
  7. DEBUG:?trying?to?connect?to?host?"imap.qq.com",?port?143,?isSSL?false??
  8. *?OK?[CAPABILITY?IMAP4?IMAP4rev1?AUTH=LOGIN?NAMESPACE]?QQMail?IMAP4Server?ready??
  9. IMAP?DEBUG:?AUTH:?LOGIN??
  10. DEBUG:?protocolConnect?login,?host=imap.qq.com,?user=<qq号码>,?password=<non-null>??
  11. A0?AUTHENTICATE?LOGIN??
  12. A0?BAD???????Ч??????????
  13. 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邮箱正常,最终程序如下

?

view plaincopy to clipboardprint?
  1. import?java.io.UnsupportedEncodingException;??
  2. import?java.util.Properties;??
  3. import?javax.mail.Authenticator;??
  4. import?javax.mail.Folder;??
  5. import?javax.mail.MessagingException;??
  6. import?javax.mail.PasswordAuthentication;??
  7. import?javax.mail.Session;??
  8. import?javax.mail.Store;??
  9. import?ce.mail.models.ConnectionProfile;?

热点排行
Bad Request.