您的位置:首页 > 编程语言 > Java开发

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

2011-01-24 12:47 579 查看
最开始的java代码如下
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐