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

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

 

最开始的java代码如下

 

  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信息如下

 

  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邮箱正常,最终程序如下

 

  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; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值