From 79bd1e4d520071a36463b0294d4957f0cde75dd5 Mon Sep 17 00:00:00 2001 From: Tyson Williams Date: Tue, 23 Apr 2019 14:11:54 -0500 Subject: [PATCH] only send 421 command when aborting the session --- src/EmbeddedMail/EmbeddedSmtpServer.cs | 7 ++++++- src/EmbeddedMail/ISmtpSession.cs | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/EmbeddedMail/EmbeddedSmtpServer.cs b/src/EmbeddedMail/EmbeddedSmtpServer.cs index 2b71029..1d6c971 100644 --- a/src/EmbeddedMail/EmbeddedSmtpServer.cs +++ b/src/EmbeddedMail/EmbeddedSmtpServer.cs @@ -115,7 +115,12 @@ public void OnClientConnect(ISocket clientSocket) { OnMessage = (msg) => _messages.Add(msg) }; - session.Start(); + + try { + session.Start(); + } catch (Exception) { + session.WriteResponse("421 localhost error occured"); + } _sessions.Add(session); } diff --git a/src/EmbeddedMail/ISmtpSession.cs b/src/EmbeddedMail/ISmtpSession.cs index 7f70b1b..d9f0acf 100644 --- a/src/EmbeddedMail/ISmtpSession.cs +++ b/src/EmbeddedMail/ISmtpSession.cs @@ -72,7 +72,8 @@ Encoding GetDefaultStreamWriterEncoding() { * ReadLine returns `null` if the end of the input stream is reached * https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader.readline */ - break; + WriteResponse("421 localhost reached end of input stream while attempting to receive the next line from the client"); + return; } var token = SmtpToken.FromLine(line, isMessageBody); @@ -81,17 +82,17 @@ Encoding GetDefaultStreamWriterEncoding() { var handler = ProtocolHandlers.HandlerFor(token); if(handler.Handle(token, this) == ContinueProcessing.Stop) { - break; + return; } isMessageBody = token.IsData && token.IsMessageBody; } + + SmtpLog.Warn("The socket closed unexpectedly"); } public void Dispose() { - WriteResponse("421 localhost Closing transmission channel"); - _writer.Close(); _reader.Close(); }