转:http://blog.csdn.net/snowclash/archive/2011/01/24/6160999.aspx
最开始的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;