diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java index cd679a5f8..9278f8708 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java @@ -555,14 +555,14 @@ class ImapConnection { private static Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory) throws GeneralSecurityException, MessagingException, IOException { // Try all IPv4 and IPv6 addresses of the host - InetAddress[] addresses = InetAddress.getAllByName(settings.getHost()); - for (int i = 0; i < addresses.length; i++) { + Exception connectException = null; + for (InetAddress address : InetAddress.getAllByName(settings.getHost())) { try { if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { - Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + addresses[i]); + Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + address); } - SocketAddress socketAddress = new InetSocketAddress(addresses[i], settings.getPort()); + SocketAddress socketAddress = new InetSocketAddress(address, settings.getPort()); Socket socket; if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) { socket = socketFactory.createSocket( @@ -576,15 +576,12 @@ class ImapConnection { socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); // Successfully connected to the server; don't try any other addresses return socket; - } catch (SocketException e) { - if (i < (addresses.length - 1)) { - // There are still other addresses for that host to try - continue; - } - throw new MessagingException("Cannot connect to host", e); + } catch (IOException e) { + Log.w(LOG_TAG, "could not connect to "+address, e); + connectException = e; } } - throw new MessagingException("Cannot connect to host"); + throw new MessagingException("Cannot connect to host", connectException); } private void adjustDNSCacheTTL() { diff --git a/k9mail/src/androidTest/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java b/k9mail/src/androidTest/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java index b34ff85f2..6465cb7cb 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java @@ -15,7 +15,6 @@ import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.MessagingException; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith;