Repository: thundernest/k-9
Issue: Error message: "unable to open connection to smtp server"

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.202

### Where did you get the app from?

F-Droid

### Android version

9

### Device model

Cat S61

### Steps to reproduce

1. Start a new account and follow the required steps.

### Expected behavior

K-9 Mail should manage to get connection to the SMTP-server as well as to the IMAP-server.

### Actual behavior

After controlling the IMAP settings K-9 Mail starts to control the SMTP settings, and after some seconds it delivers the error message "unable to open connection to smtp server".

No problem at all with Thunderbird (on Laptop).

### Logs

[k9mail-logs[1].txt](https://github.com/thundernest/k-9/files/9732971/k9mail-logs.1.txt)


Changed files:
mail/protocols/smtp/src/main/java/com/fsck/k9/mail/transport/smtp/SmtpTransport.kt

diff --git a/mail/protocols/smtp/src/main/java/com/fsck/k9/mail/transport/smtp/SmtpTransport.kt b/mail/protocols/smtp/src/main/java/com/fsck/k9/mail/transport/smtp/SmtpTransport.kt
index 43c78cc49..fbec8e715 100644
--- a/mail/protocols/smtp/src/main/java/com/fsck/k9/mail/transport/smtp/SmtpTransport.kt
+++ b/mail/protocols/smtp/src/main/java/com/fsck/k9/mail/transport/smtp/SmtpTransport.kt
@@ -33,7 +33,6 @@ import java.net.Inet6Address
 import java.net.InetAddress
 import java.net.InetSocketAddress
 import java.net.Socket
-import java.net.SocketException
 import java.security.GeneralSecurityException
 import java.util.Locale
 import javax.net.ssl.SSLException
@@ -83,35 +82,10 @@ class SmtpTransport(
     @Throws(MessagingException::class)
     override fun open() {
         try {
-            var secureConnection = false
-            val addresses = InetAddress.getAllByName(host)
-            for ((index, address) in addresses.withIndex()) {
-                try {
-                    val socketAddress = InetSocketAddress(address, port)
-                    if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
-                        socket = trustedSocketFactory.createSocket(null, host, port, clientCertificateAlias).also {
-                            it.connect(socketAddress, SOCKET_CONNECT_TIMEOUT)
-                        }
-                        secureConnection = true
-                    } else {
-                        socket = Socket().also {
-                            it.connect(socketAddress, SOCKET_CONNECT_TIMEOUT)
-                        }
-                    }
-                } catch (e: SocketException) {
-                    if (index < addresses.lastIndex) {
-                        // there are still other addresses for that host to try
-                        continue
-                    }
-
-                    throw MessagingException("Cannot connect to host", e)
-                }
+            var secureConnection = connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED
 
-                // connection success
-                break
-            }
-
-            val socket = this.socket ?: error("socket == null")
+            val socket = connect()
+            this.socket = socket
 
             socket.soTimeout = SOCKET_READ_TIMEOUT
 
@@ -263,6 +237,39 @@ class SmtpTransport(
         }
     }
 
+    private fun connect(): Socket {
+        val inetAddresses = InetAddress.getAllByName(host)
+
+        var connectException: Exception? = null
+        for (address in inetAddresses) {
+            connectException = try {
+                return connectToAddress(address)
+            } catch (e: IOException) {
+                Timber.w(e, "Could not connect to %s", address)
+                e
+            }
+        }
+
+        throw MessagingException("Cannot connect to host", connectException)
+    }
+
+    private fun connectToAddress(address: InetAddress): Socket {
+        if (K9MailLib.isDebug() && K9MailLib.DEBUG_PROTOCOL_SMTP) {
+            Timber.d("Connecting to %s as %s", host, address)
+        }
+
+        val socketAddress = InetSocketAddress(address, port)
+        val socket = if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
+            trustedSocketFactory.createSocket(null, host, port, clientCertificateAlias)
+        } else {
+            Socket()
+        }
+
+        socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT)
+
+        return socket
+    }
+
     private fun readGreeting() {
         val smtpResponse = responseParser!!.readGreeting()
         logResponse(smtpResponse)