Cannot connect to server (Invalid CAPABILITY response received)
### Expected behavior
Fetch mail

### Actual behavior
Cannot connect to server (Invalid CAPABILITY response received)

### Steps to reproduce
Capabilities returned from the IMAP server
```
a capability
* CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN AUTH=LOGIN ID APPENDLIMIT
a OK CAPABILITY completed
```

### Environment
K-9 Mail version: 5.600

Account type (IMAP, POP3, WebDAV/Exchange): IMAP

Please take some time to [retrieve logs](https://github.com/k9mail/k-9/wiki/LoggingErrors) and attach them here:

TBD

diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java
index dde67c72f..3b866a0c4 100644
--- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java
+++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java
@@ -265,31 +265,24 @@ class ImapConnection {
         extractCapabilities(Collections.singletonList(initialResponse));
     }
 
-    private List<ImapResponse> extractCapabilities(List<ImapResponse> responses) {
+    private boolean extractCapabilities(List<ImapResponse> responses) {
         CapabilityResponse capabilityResponse = CapabilityResponse.parse(responses);
-        if (capabilityResponse != null) {
-            Set<String> receivedCapabilities = capabilityResponse.getCapabilities();
-            if (K9MailLib.isDebug()) {
-                Timber.d("Saving %s capabilities for %s", receivedCapabilities, getLogId());
-            }
-            capabilities = receivedCapabilities;
+        if (capabilityResponse == null) {
+            return false;
         }
-        return responses;
+
+        Set<String> receivedCapabilities = capabilityResponse.getCapabilities();
+        Timber.d("Saving %s capabilities for %s", receivedCapabilities, getLogId());
+        capabilities = receivedCapabilities;
+
+        return true;
     }
 
-    private List<ImapResponse> extractOrRequestCapabilities(List<ImapResponse> responses)
-            throws IOException, MessagingException {
-        CapabilityResponse capabilityResponse = CapabilityResponse.parse(responses);
-        if (capabilityResponse != null) {
-            Set<String> receivedCapabilities = capabilityResponse.getCapabilities();
-            Timber.d("Saving %s capabilities for %s", receivedCapabilities, getLogId());
-            capabilities = receivedCapabilities;
-        } else {
+    private void extractOrRequestCapabilities(List<ImapResponse> responses) throws IOException, MessagingException {
+        if (!extractCapabilities(responses)) {
             Timber.i("Did not get capabilities in post-auth banner, requesting CAPABILITY for %s", getLogId());
             requestCapabilities();
         }
-
-        return responses;
     }
 
     private void requestCapabilitiesIfNecessary() throws IOException, MessagingException {
@@ -303,8 +296,7 @@ class ImapConnection {
     }
 
     private void requestCapabilities() throws IOException, MessagingException {
-        List<ImapResponse> responses = extractCapabilities(executeSimpleCommand(Commands.CAPABILITY));
-        if (responses.size() != 2) {
+        if (!extractCapabilities(executeSimpleCommand(Commands.CAPABILITY))) {
             throw new MessagingException("Invalid CAPABILITY response received");
         }
     }
diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java
index 6ace4f139..281df9be0 100644
--- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java
+++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java
@@ -691,6 +691,30 @@ public class ImapConnectionTest {
         server.verifyInteractionCompleted();
     }
 
+    @Test
+    public void open_withUntaggedCapabilityAfterStartTls_shouldNotThrow() throws Exception {
+        settings.setAuthType(AuthType.PLAIN);
+        settings.setConnectionSecurity(ConnectionSecurity.STARTTLS_REQUIRED);
+        MockImapServer server = new MockImapServer();
+        preAuthenticationDialog(server, "STARTTLS LOGINDISABLED");
+        server.expect("2 STARTTLS");
+        server.output("2 OK Begin TLS negotiation now");
+        server.startTls();
+        server.output("* CAPABILITY IMAP4REV1 IMAP4");
+        server.expect("3 CAPABILITY");
+        server.output("* CAPABILITY IMAP4 IMAP4REV1");
+        server.output("3 OK");
+        server.expect("4 LOGIN \\"" + USERNAME + "\\" \\"" + PASSWORD + "\\"");
+        server.output("4 OK [CAPABILITY IMAP4REV1] LOGIN completed");
+        simplePostAuthenticationDialog(server, 5);
+        ImapConnection imapConnection = startServerAndCreateImapConnection(server);
+
+        imapConnection.open();
+
+        server.verifyConnectionStillOpen();
+        server.verifyInteractionCompleted();
+    }
+
     @Test
     public void open_withNegativeResponseToStartTlsCommand_shouldThrow() throws Exception {
         settings.setAuthType(AuthType.PLAIN);
['mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java', 'mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java']