public async Task ProcessAsync(IConnection connection, SmtpCommand command) { SmtpCommand subrequest = new SmtpCommand(command.ArgumentsText); IVerb verbProcessor = SubVerbMap.GetVerbProcessor(subrequest.Verb); if (verbProcessor != null) { await verbProcessor.ProcessAsync(connection, subrequest); } else { await connection.WriteResponseAsync( new SmtpResponse(StandardSmtpResponseCode.CommandParameterNotImplemented, "Subcommand {0} not implemented", subrequest.Verb)); } }
public async Task ProcessAsync() { try { Server.Behaviour.OnSessionStarted(this, Session); SetReaderEncoding(Server.Behaviour.GetDefaultEncoding(this)); if (Server.Behaviour.IsSSLEnabled(this)) { await ConnectionChannel.ApplyStreamFilterAsync(async s => { SslStream sslStream = new SslStream(s); await sslStream.AuthenticateAsServerAsync(Server.Behaviour.GetSSLCertificate(this)); return(sslStream); }); Session.SecureConnection = true; } await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.ServiceReady, Server.Behaviour.DomainName + " smtp4dev ready")); int numberOfInvalidCommands = 0; while (ConnectionChannel.IsConnected) { bool badCommand = false; SmtpCommand command = new SmtpCommand(await ReadLineAsync()); Server.Behaviour.OnCommandReceived(this, command); if (command.IsValid) { IVerb verbProcessor = VerbMap.GetVerbProcessor(command.Verb); if (verbProcessor != null) { try { await verbProcessor.ProcessAsync(this, command); } catch (SmtpServerException exception) { await WriteResponseAsync(exception.SmtpResponse); } } else { badCommand = true; } } else if (command.IsEmpty) { } else { badCommand = true; } if (badCommand) { numberOfInvalidCommands++; if (Server.Behaviour.MaximumNumberOfSequentialBadCommands > 0 && numberOfInvalidCommands >= Server.Behaviour.MaximumNumberOfSequentialBadCommands) { await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.ClosingTransmissionChannel, "Too many bad commands. Bye!")); await CloseConnectionAsync(); } else { await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised, "Command unrecognised")); } } } } catch (IOException ioException) { Session.SessionError = ioException; Session.SessionErrorType = SessionErrorType.NetworkError; } catch (Exception exception) { Session.SessionError = exception; Session.SessionErrorType = SessionErrorType.UnexpectedException; } await CloseConnectionAsync(); Session.EndDate = DateTime.Now; Server.Behaviour.OnSessionCompleted(this, Session); }