/// <summary> /// Handles the command input from the client. This /// message returns when the client issues the quit command. /// </summary> private void ProcessCommands(SMTPContext context) { bool isRunning = true; // Loop until the client quits. while (isRunning) { try { String inputLine = context.ReadLine(); if (inputLine == null) { isRunning = false; context.Close(); continue; } Log.DebugFormat(Resources.Log_ProcessCommands_ProcessCommands_Read_0, inputLine); String[] inputs = inputLine.Split(" ".ToCharArray()); var messageUnknownCommand = Resources.Protocol_MESSAGE_UNKNOWN_COMMAND_500_Command_Unrecognized; switch (inputs[0].ToLower()) { case "helo": Helo(context, inputs); break; case "rset": Rset(context); break; case "noop": context.WriteLine(Resources.Protocol_MESSAGE_OK_250_OK); break; case "quit": isRunning = false; context.WriteLine(Resources.Protocol_MESSAGE_GOODBYE_221_Goodbye); context.Close(); break; case "mail": if (inputs[1].ToLower().StartsWith("from")) { Mail(context, inputLine.Substring(inputLine.IndexOf(" ", StringComparison.Ordinal))); break; } context.WriteLine(messageUnknownCommand); break; case "rcpt": if (inputs[1].ToLower().StartsWith("to")) { Rcpt(context, inputLine.Substring(inputLine.IndexOf(" ", StringComparison.Ordinal))); break; } context.WriteLine(messageUnknownCommand); break; case "data": Data(context); break; default: context.WriteLine(messageUnknownCommand); break; } } catch (Exception exception) { Log.ErrorFormat( Resources.Log_ProcessCommands_Connection_0_Exception_occured_while_processing_commands_1, context.ConnectionId, exception, exception); context.WriteLine(Resources.Protocol_MESSAGE_SYSTEM_ERROR_554_Transaction_failed); } } }
private void Data(SMTPContext context) { context.WriteLine(Resources.Protocol_MESSAGE_START_DATA__354_Start_mail_input_end_with_CRLF_CRLF); SMTPMessage message = context.Message; var clientEndPoint = (IPEndPoint)context.Socket.RemoteEndPoint; var header = new StringBuilder(); header.AppendFormat(Resources.EMAIL_Received_from_0_0_1, context.ClientDomain, clientEndPoint.Address); header.AppendLine(); header.AppendFormat(Resources.EMAIL_by_0_Eric_Daugherty_s_C_Email_Server, _domain); header.AppendLine(); header.Append(" " + DateTime.Now); header.AppendLine(); message.AddData(header.ToString()); String line = context.ReadLine(); while (!line.Equals(".")) { message.AddData(line); message.AddData("\r\n"); line = context.ReadLine(); } // Spool the message _messageSpool.SpoolMessage(message); context.WriteLine(Resources.Protocol_MESSAGE_OK_250_OK); // Reset the connection. context.Reset(); }