From 696666ca5e04c841c832cae0e5068ec1224ad1f0 Mon Sep 17 00:00:00 2001 From: mwolschon Date: Thu, 15 Dec 2011 10:40:22 +0100 Subject: [PATCH] Issue 1303: can't send mail get "no route to host" error --- src/com/fsck/k9/mail/store/ImapStore.java | 49 ++++++++++++------- .../fsck/k9/mail/transport/SmtpTransport.java | 2 +- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index 2c9f2371e..096b7e232 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -9,6 +9,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.ConnectException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; @@ -2132,24 +2133,36 @@ public class ImapStore extends Store { try { - - SocketAddress socketAddress = new InetSocketAddress(mSettings.getHost(), mSettings.getPort()); - - if (K9.DEBUG) - Log.i(K9.LOG_TAG, "Connection " + getLogId() + " connecting to " + mSettings.getHost() + " @ IP addr " + socketAddress); - - if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_REQUIRED || - mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_OPTIONAL) { - SSLContext sslContext = SSLContext.getInstance("TLS"); - final boolean secure = mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_REQUIRED; - sslContext.init(null, new TrustManager[] { - TrustManagerFactory.get(mSettings.getHost(), secure) - }, new SecureRandom()); - mSocket = sslContext.getSocketFactory().createSocket(); - mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); - } else { - mSocket = new Socket(); - mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + // -> try all IPv4 and IPv6 addresses of the host. + int mConnectionSecurity = mSettings.getConnectionSecurity(); + InetAddress[] addresses = InetAddress.getAllByName(mSettings.getHost()); + for (int i = 0; i < addresses.length; i++) { + try { + if (K9.DEBUG && K9.DEBUG_PROTOCOL_IMAP) { + Log.d(K9.LOG_TAG, "connecting to " + mSettings.getHost() + " as " + addresses[i]); + } + SocketAddress socketAddress = new InetSocketAddress(addresses[i], mSettings.getPort()); + if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || + mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) { + SSLContext sslContext = SSLContext.getInstance("TLS"); + boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; + sslContext.init(null, new TrustManager[] { + TrustManagerFactory.get(mSettings.getHost(), secure) + }, new SecureRandom()); + mSocket = sslContext.getSocketFactory().createSocket(); + mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + } else { + mSocket = new Socket(); + mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + } + } 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); + } + break; // connection success } setReadTimeout(Store.SOCKET_READ_TIMEOUT); diff --git a/src/com/fsck/k9/mail/transport/SmtpTransport.java b/src/com/fsck/k9/mail/transport/SmtpTransport.java index 81e9b258d..3840d362f 100644 --- a/src/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/src/com/fsck/k9/mail/transport/SmtpTransport.java @@ -250,7 +250,7 @@ public class SmtpTransport extends Transport { mSocket = new Socket(); mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); } - } catch (ConnectException e) { + } catch (SocketException e) { if (i < (addresses.length - 1)) { // there are still other addresses for that host to try continue;