Merge pull request #6049 from k9mail/fix_smtp_starttls
Fix SMTP not working when STARTTLS is configured
This commit is contained in:
commit
e619d38143
4 changed files with 45 additions and 9 deletions
|
@ -2,12 +2,6 @@
|
|||
package com.fsck.k9.mail;
|
||||
|
||||
public abstract class Transport {
|
||||
|
||||
protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
|
||||
|
||||
// RFC 1047
|
||||
protected static final int SOCKET_READ_TIMEOUT = 300000;
|
||||
|
||||
public abstract void open() throws MessagingException;
|
||||
|
||||
public abstract void sendMessage(Message message) throws MessagingException;
|
||||
|
|
|
@ -10,6 +10,8 @@ import com.fsck.k9.mail.K9MailLib
|
|||
import com.fsck.k9.mail.Message
|
||||
import com.fsck.k9.mail.Message.RecipientType
|
||||
import com.fsck.k9.mail.MessagingException
|
||||
import com.fsck.k9.mail.NetworkTimeouts.SOCKET_CONNECT_TIMEOUT
|
||||
import com.fsck.k9.mail.NetworkTimeouts.SOCKET_READ_TIMEOUT
|
||||
import com.fsck.k9.mail.ServerSettings
|
||||
import com.fsck.k9.mail.Transport
|
||||
import com.fsck.k9.mail.filter.Base64
|
||||
|
@ -128,15 +130,16 @@ class SmtpTransport(
|
|||
if (extensions.containsKey("STARTTLS")) {
|
||||
executeCommand("STARTTLS")
|
||||
|
||||
this.socket = trustedSocketFactory.createSocket(
|
||||
val tlsSocket = trustedSocketFactory.createSocket(
|
||||
socket,
|
||||
host,
|
||||
port,
|
||||
clientCertificateAlias
|
||||
)
|
||||
inputStream = PeekableInputStream(BufferedInputStream(socket.getInputStream(), 1024))
|
||||
this.socket = tlsSocket
|
||||
inputStream = PeekableInputStream(BufferedInputStream(tlsSocket.getInputStream(), 1024))
|
||||
responseParser = SmtpResponseParser(logger, inputStream!!)
|
||||
outputStream = BufferedOutputStream(socket.getOutputStream(), 1024)
|
||||
outputStream = BufferedOutputStream(tlsSocket.getOutputStream(), 1024)
|
||||
|
||||
// Now resend the EHLO. Required by RFC2487 Sec. 5.2, and more specifically, Exim.
|
||||
extensions = sendHello(helloName)
|
||||
|
|
|
@ -68,6 +68,11 @@ public class MockSmtpServer {
|
|||
interactions.add(new ExpectedCommand(command));
|
||||
}
|
||||
|
||||
public void startTls() {
|
||||
checkServerNotRunning();
|
||||
interactions.add(new UpgradeToTls());
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
checkServerNotRunning();
|
||||
interactions.add(new CloseConnection());
|
||||
|
@ -212,6 +217,9 @@ public class MockSmtpServer {
|
|||
}
|
||||
}
|
||||
|
||||
private static class UpgradeToTls implements SmtpInteraction {
|
||||
}
|
||||
|
||||
private static class CloseConnection implements SmtpInteraction {
|
||||
}
|
||||
|
||||
|
@ -303,6 +311,8 @@ public class MockSmtpServer {
|
|||
readExpectedCommand((ExpectedCommand) interaction);
|
||||
} else if (interaction instanceof CannedResponse) {
|
||||
writeCannedResponse((CannedResponse) interaction);
|
||||
} else if (interaction instanceof UpgradeToTls) {
|
||||
upgradeToTls(socket);
|
||||
} else if (interaction instanceof CloseConnection) {
|
||||
clientSocket.close();
|
||||
}
|
||||
|
|
|
@ -567,6 +567,35 @@ class SmtpTransportTest {
|
|||
server.verifyInteractionCompleted()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `open() with STARTTLS`() {
|
||||
val server = MockSmtpServer().apply {
|
||||
output("220 localhost Simple Mail Transfer Service Ready")
|
||||
expect("EHLO [127.0.0.1]")
|
||||
output("250-localhost Hello 127.0.0.1")
|
||||
output("250-STARTTLS")
|
||||
output("250 HELP")
|
||||
expect("STARTTLS")
|
||||
output("220 Ready to start TLS")
|
||||
startTls()
|
||||
expect("EHLO [127.0.0.1]")
|
||||
output("250-localhost Hello 127.0.0.1")
|
||||
output("250 AUTH PLAIN LOGIN")
|
||||
expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=")
|
||||
output("235 2.7.0 Authentication successful")
|
||||
}
|
||||
val transport = startServerAndCreateSmtpTransport(
|
||||
server,
|
||||
authenticationType = AuthType.PLAIN,
|
||||
connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED
|
||||
)
|
||||
|
||||
transport.open()
|
||||
|
||||
server.verifyConnectionStillOpen()
|
||||
server.verifyInteractionCompleted()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sendMessage() without address to send to should not open connection`() {
|
||||
val message = MimeMessage()
|
||||
|
|
Loading…
Reference in a new issue