1
votes

I am trying to connect with iCloud mail using Java app. I want to read mails from iCloud IMAP mail server using user credentials. But it does not works. Below code snippet worked for Gmail, Yahoo, Outlook, but not working for iCloud:

public void connectToIMAP(final User user, MailSettings settings) {     
    ICloudSettings iCloud = (ICloudSettings) settings;

    properties = System.getProperties();
    properties.setProperty("mail.store.protocol", iCloud.imap.protocol);
    properties.setProperty("mail.imap.starttls.enable", "true");
    properties.setProperty("mail.imap.debug", "true");
    try {
        session = Session.getInstance(properties);
        session.setDebug(true);         
        store = session.getStore();         
        store.connect(iCloud.imap.host, iCloud.imap.port, user.getEmail(), user.getPassword());
        folder = store.getFolder(MailSettings.INBOX);
        folder.open(Folder.READ_ONLY);          
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public ICloudSettings() {
    imap.host = "imap.mail.me.com";
    imap.port = 993;
    imap.protocol = "imaps";
}

Debug info is:

DEBUG: setDebug: JavaMail version 1.5.4

DEBUG: getProvider() returning

javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]

DEBUG IMAPS: mail.imap.fetchsize: 16384

DEBUG IMAPS: mail.imap.ignorebodystructuresize: false

DEBUG IMAPS: mail.imap.statuscachetimeout: 1000

DEBUG IMAPS: mail.imap.appendbuffersize: -1

DEBUG IMAPS: mail.imap.minidletime: 10

DEBUG IMAPS: closeFoldersOnStoreFailure

DEBUG IMAPS: trying to connect to host "imap.mail.me.com", port 993, isSSL true * OK [CAPABILITY mr11p00im-iscream006 15E43 XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN] iSCREAM ready to rumble (15E43-20056:9352) mr11p00im-iscream006 [24:210:09:33:11:22]

DEBUG IMAPS: AUTH: ATOKEN

DEBUG IMAPS: AUTH: PLAIN

DEBUG IMAPS: protocolConnect login, host=imap.mail.me.com, [email protected], password=

DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed

DEBUG IMAPS: AUTHENTICATE PLAIN command result: A0 NO [AUTHENTICATIONFAILED] Authentication failed javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Authentication failed at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:723) at javax.mail.Service.connect(Service.java:364)

2
It didn't like your credentials. Double check your username and password. Make sure they work in a standard IMAP client.Max
@Max Credentials are correct, I verified my credentials on browser and it works perfectly.user2618875

2 Answers

2
votes

Hi I'm also connecting to iCloud via IMAP, but not with java mail. I'm using piece of code from K9 mail client, available at github. I find out that accessing IMAP does not work when you have 2 factor authorization turned on for your icloud account. Second scenario when I get similar message to you (Authentication failed) is when I use email address different then @icloud.com one. For example I set up my apple id with my gmail account, and created @icloud.com alias. I cannot access IMAP with gmail address as username, but there is no problem when I use @icloud.com alias.

1
votes

If you have enabled 2 Factor authentication, you need to have app-specific password created. Use the app-specific password instead of standard Apple ID password.

Also, if your Apple ID ends with @me.com (say [email protected]), then IMAP username should be just the username part of the email address (for email => [email protected], IMAP username => foo)