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.

This entry was posted in Java and tagged , , . Bookmark the permalink.

10 Responses to Connect Yahoo IMAP Server by JavaMail

  1. Paulo says:

    Hi. great post, helped me a lot.

    I connect with no problems using “imap” but with “imaps” i get:
    A2 BAD Unknown command

    Any idea why this might be happening?
    Thanks.

    BTW, i believe you made a mistake:
    YahooIMAPStore should extend IMAPStore and
    YahooIMAPSSLStore should extend IMAPSSLStore and not otherwise, right?

    cheers.

  2. Zhou Renjian says:

    @Paulo
    Thanks for point out the mistake!
    And the imap server is updated to “imap.mail.yahoo.com” with IMAP port 143.
    It seems that Yahoo! stopped supporting “imap.next.yahoo.com” some days ago.

  3. Bill Shannon says:

    FYI, the latest JavaMail 1.4.4-SNAPSHOT release supports this directly.
    See this page for more info:
    http://kenai.com/projects/javamail/pages/Yahoo

    • Makayla Black says:

      Hi Bill – I just tried your new 1.4.4 JAR and it works great. Thanks for helping to get the world connected! — Miki

  4. Zhou Renjian says:

    @Bill Shannon
    Thanks for notifying such updates.

  5. Peter says:

    Yahoo still supports free Yahoo Imap, but you need a “Yahoo Imap” cabable email client. There is none at the moment for the desktop (other than the bloated zimbra client …)

    Byt you can use a special version of Thunderbird 3.0 or 3.1 to read Yahoo Imap securely via IMAP (ssl). The patch and binaries are available at

    http://www.aliasbailbonds.com/KeeForm/category/thunderbird

  6. Zhou Renjian says:

    @Peter
    Thanks for your information.
    http://kexmail.com/ ‘s web mail client supports Yahoo IMAP. I don’t like desktop mail client right now.

  7. piyush says:

    I am getting this message
    javax.mail.MessagingException: No login methods supported

  8. piyush says:

    contd..
    i have a email with domain as yahoo.co.in
    and rest i am following all the steps given by Zhou Renjian
    but getting the above message

  9. Raja usk says:

    Thank you sir…
    Your Effort more helpful to me….