Issue 1303: can't send mail get "no route to host" error

This commit is contained in:
mwolschon 2011-12-15 10:40:22 +01:00 committed by cketti
parent 21c9c6c1dc
commit 696666ca5e
2 changed files with 32 additions and 19 deletions

View file

@ -9,6 +9,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
@ -2132,16 +2133,19 @@ public class ImapStore extends Store {
try { try {
// -> try all IPv4 and IPv6 addresses of the host.
SocketAddress socketAddress = new InetSocketAddress(mSettings.getHost(), mSettings.getPort()); int mConnectionSecurity = mSettings.getConnectionSecurity();
InetAddress[] addresses = InetAddress.getAllByName(mSettings.getHost());
if (K9.DEBUG) for (int i = 0; i < addresses.length; i++) {
Log.i(K9.LOG_TAG, "Connection " + getLogId() + " connecting to " + mSettings.getHost() + " @ IP addr " + socketAddress); try {
if (K9.DEBUG && K9.DEBUG_PROTOCOL_IMAP) {
if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_REQUIRED || Log.d(K9.LOG_TAG, "connecting to " + mSettings.getHost() + " as " + addresses[i]);
mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_OPTIONAL) { }
SocketAddress socketAddress = new InetSocketAddress(addresses[i], mSettings.getPort());
if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED ||
mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
final boolean secure = mSettings.getConnectionSecurity() == CONNECTION_SECURITY_SSL_REQUIRED; boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED;
sslContext.init(null, new TrustManager[] { sslContext.init(null, new TrustManager[] {
TrustManagerFactory.get(mSettings.getHost(), secure) TrustManagerFactory.get(mSettings.getHost(), secure)
}, new SecureRandom()); }, new SecureRandom());
@ -2151,6 +2155,15 @@ public class ImapStore extends Store {
mSocket = new Socket(); mSocket = new Socket();
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); 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); setReadTimeout(Store.SOCKET_READ_TIMEOUT);

View file

@ -250,7 +250,7 @@ public class SmtpTransport extends Transport {
mSocket = new Socket(); mSocket = new Socket();
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
} }
} catch (ConnectException e) { } catch (SocketException e) {
if (i < (addresses.length - 1)) { if (i < (addresses.length - 1)) {
// there are still other addresses for that host to try // there are still other addresses for that host to try
continue; continue;