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)