Monthly Archives: November 2009

Connect Yahoo IMAP Server by JavaMail

These days, I were implementing an online web mail client for all mail services, like Gmail, Hotmail/Live Mail, Yahoo! Mail, AOL/AIM Mail. And adding Yahoo! Mail support cost me lots of time, but the solution is simple.

Yahoo! Mail provides only web access for free users and does not provide free POP3 services. You need to buy Yahoo! Mail Plus to have POP support. Not long before, Yahoo! Mail provided IMAP services for Zoho Web Mail. It means that IMAP support is free for any Yahoo! Mail users, no Yahoo! Mail Plus is needed. But Yahoo! Mail’s IMAP services is not a totally standard IMAP service. You need send some special tokens before you login. So a customized Thunderbird is need to Yahoo! IMAP support.

JavaMail provides a convenient way to connect mail servers by POP/IMAP/SMTP and others. Here is a “how-to” of making a little modification of JavaMail library to support Yahoo! Mail’s IMAP service.

1. Download sources of JavaMail
2. Modify com.sun.mail.imap.IMAPStore.login(IMAPProtocol, String, String) from a private method into a protected method, because we need to override it for Yahoo! IMAP.
3. Replace the compiled IMAPStore*.class files in JavaMail’s binary library file mail.jar.
4. Write two classes:

public class YahooIMAPStore extends IMAPStore {

	public YahooIMAPStore(Session session, URLName url) {
		super(session, url);
	}

	@Override
	protected void login(IMAPProtocol p, String u, String pw)
			throws ProtocolException {
		Response[] r = p.command("ID (\"GUID\" \"1\")", null);
		p.notifyResponseHandlers(r);
		
		super.login(p, u, pw);
	}
}

public class YahooIMAPSSLStore extends IMAPSSLStore {

	public YahooIMAPSSLStore(Session session, URLName url) {
		super(session, url);
	}

	@Override
	protected void login(IMAPProtocol p, String u, String pw)
			throws ProtocolException {
		Response[] r = p.command("ID (\"GUID\" \"1\")", null);
		p.notifyResponseHandlers(r);
		
		super.login(p, u, pw);
	}
}

5. Modify JavaMail related codes to use overrided YahooIMAPStore:

if ("yahoo.com".equals(domain)
		|| "ymail.com".equals(domain)
		|| "rocketmail.cn".equals(domain)) {
	if (protocol == null) {
		protocol = "imaps";
		host = "imap.next.mail.yahoo.com";
		port = 993;

		protocol = "imap";
		host = "imap.mail.yahoo.com";
		port = 143;
	}
	if (sendProtocol == null) {
		sendProtocol = "smtps";
		sendHost = "smtp.mail.yahoo.com";
		sendPort = 587;
	}
}
...
if (host.indexOf(".yahoo.com") != -1) {
	props.setProperty("mail.imap.class", "im.webuzz.mail.YahooIMAPStore");
	props.setProperty("mail.imaps.class", "im.webuzz.mail.YahooIMAPSSLStore");
}
Session session = Session.getInstance(props, null);
if (host.indexOf(".yahoo.com") != -1) {
	session.addProvider(new Provider(Provider.Type.STORE, "imap",
		"im.webuzz.mail.YahooIMAPStore", "WeBuzz", "1.0"));
	session.addProvider(new Provider(Provider.Type.STORE, "imaps",
		"im.webuzz.mail.YahooIMAPSSLStore", "WeBuzz", "1.0"));
}

store = session.getStore(protocol);
store.connect(host, user, password);
...

Done! Enjoy Yahoo! Mail’s IMAP support by JavaMail.

BTW: You can test out WeBuzz’s Web Mail for Yahoo! Mail, which is using JavaMail and IMAP with the above hacks.

Posted in Java | Tagged , , | 37 Comments