SMTP: Don't attempt to send QUIT command when connection is closed
@@ -363,7 +363,7 @@ class SmtpTransport(
message.removeHeader("Bcc")
- close()
+ ensureClosed()
open()
// If the message has attachments and our server has told us about a limit on the size of messages, count
@@ -434,12 +434,16 @@ class SmtpTransport(
}
}
- override fun close() {
- try {
- executeCommand("QUIT")
- } catch (ignored: Exception) {
+ private fun ensureClosed() {
+ if (inputStream != null || outputStream != null || socket != null || responseParser != null) {
+ Timber.w(RuntimeException(), "SmtpTransport was open when it was expected to be closed")
+ close()
+ }
}
+ override fun close() {
+ writeQuitCommand()
+
IOUtils.closeQuietly(inputStream)
IOUtils.closeQuietly(outputStream)
IOUtils.closeQuietly(socket)
@@ -450,6 +454,14 @@ class SmtpTransport(
socket = null
}
+ private fun writeQuitCommand() {
+ try {
+ // We don't care about the server's response to the QUIT command
+ writeLine("QUIT")
+ } catch (ignored: Exception) {
+ }
+ }
+
private fun writeLine(command: String, sensitive: Boolean = false) {
if (K9MailLib.isDebug() && K9MailLib.DEBUG_PROTOCOL_SMTP) {
val commandToLog = if (sensitive && !K9MailLib.isDebugSensitive()) {
@@ -637,7 +649,7 @@ class SmtpTransport(
@Throws(MessagingException::class)
fun checkSettings() {
- close()
+ ensureClosed()
try {
open()
