private void Data(SMTPContext context) { context.WriteLine(MESSAGE_START_DATA); SMTPMessage message = context.Message; IPEndPoint clientEndPoint = (IPEndPoint)context.Socket.RemoteEndPoint; StringBuilder header = new StringBuilder(); header.Append(String.Format("Received: from {0} ({0} [{1}])", context.ClientDomain, clientEndPoint.Address)); header.Append("\r\n"); header.Append(String.Format(" by {0} (Eric Daugherty's C# Email Server)", domain)); header.Append("\r\n"); header.Append(" " + System.DateTime.Now); header.Append("\r\n"); 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(MESSAGE_OK); // Reset the connection. context.Reset(); }
/// <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; String inputLine; // Loop until the client quits. while (isRunning) { try { inputLine = context.ReadLine(); if (inputLine == null) { isRunning = false; context.Close(); continue; } log.Debug("ProcessCommands Read: " + inputLine); String[] inputs = inputLine.Split(" ".ToCharArray()); switch (inputs[0].ToLower()) { case "helo": Helo(context, inputs); break; case "rset": Rset(context); break; case "noop": context.WriteLine(MESSAGE_OK); break; case "quit": isRunning = false; context.WriteLine(MESSAGE_GOODBYE); context.Close(); break; case "mail": if (inputs[1].ToLower().StartsWith("from")) { Mail(context, inputLine.Substring(inputLine.IndexOf(" "))); break; } context.WriteLine(MESSAGE_UNKNOWN_COMMAND); break; case "rcpt": if (inputs[1].ToLower().StartsWith("to")) { Rcpt(context, inputLine.Substring(inputLine.IndexOf(" "))); break; } context.WriteLine(MESSAGE_UNKNOWN_COMMAND); break; case "data": Data(context); break; default: context.WriteLine(MESSAGE_UNKNOWN_COMMAND); break; } } catch (Exception exception) { log.Error(String.Format("Connection {0}: Exception occured while processing commands: {1}", context.ConnectionId, exception), exception); context.WriteLine(MESSAGE_SYSTEM_ERROR); } } }