/// <summary> /// Update the headers or body depending on the SmtpResponse object and line of input. /// </summary> /// <param name="response">SmtpResponse object</param> /// <param name="commandData">remainder of input line after SMTP command has been removed</param> internal void Store(SmtpResponse response, string commandData) { if (commandData != null) { if (SmtpState.DATA_HDR == response.NextState) { int headerNameEnd = commandData.IndexOf(":"); if (headerNameEnd >= 0) { string name = commandData.Substring(0, (headerNameEnd) - (0)).Trim(); string value_Renamed = commandData.Substring(headerNameEnd + 1).Trim(); // We use the Add method instead of [] because we can have multiple values for a name headers.Add(name, value_Renamed); } } else if (SmtpState.DATA_BODY == response.NextState) { if (bodyLineCount > 0) { body.Append(CR); } body.Append(commandData); bodyLineCount++; } } }
/// <summary> /// Send response to client. /// </summary> /// <param name="output">socket output stream</param> /// <param name="smtpResponse">Response to send</param> private void SendResponse(StreamWriter output, SmtpResponse smtpResponse) { if (smtpResponse.Code > 0) { output.WriteLine(smtpResponse.Code + " " + smtpResponse.Message); output.Flush(); } }
/// <summary> /// Handle an SMTP transaction, i.e. all activity between initial connect and QUIT command. /// </summary> /// <param name="output">output stream</param> /// <param name="input">input stream</param> /// <returns>List of received SmtpMessages</returns> private IList HandleSmtpTransaction(StreamWriter output, StreamReader input) { // Initialize the state machine SmtpState smtpState = SmtpState.CONNECT; SmtpRequest smtpRequest = new SmtpRequest(SmtpActionType.CONNECT, String.Empty, smtpState); // Execute the connection request SmtpResponse smtpResponse = smtpRequest.Execute(); // Send initial response SendResponse(output, smtpResponse); smtpState = smtpResponse.NextState; IList msgList = new ArrayList(); SmtpMessage msg = new SmtpMessage(); while (smtpState != SmtpState.CONNECT && smtpState != SmtpState.QUIT) { string line = input.ReadLine(); if (line == null) { break; } // Create request from client input and current state SmtpRequest request = SmtpRequest.CreateRequest(line, smtpState); // Execute request and create response object SmtpResponse response = request.Execute(); // Move to next internal state smtpState = response.NextState; // Send reponse to client SendResponse(output, response); // Store input in message msg.Store(response, request.Params); // If message reception is complete save it if (smtpState == SmtpState.QUIT) { msgList.Add(msg); msg = new SmtpMessage(); } } return(msgList); }
/// <summary> /// Update the headers or body depending on the SmtpResponse object and line of input. /// </summary> /// <param name="response">SmtpResponse object</param> /// <param name="commandData">remainder of input line after SMTP command has been removed</param> internal void Store(SmtpResponse response, string commandData) { if (commandData != null) { if (SmtpState.DATA_HDR == response.NextState) { int headerNameEnd = commandData.IndexOf(":"); if (headerNameEnd >= 0) { string name = commandData.Substring(0, (headerNameEnd) - (0)).Trim(); string value_Renamed = commandData.Substring(headerNameEnd + 1).Trim(); // We use the Add method instead of [] because we can have multiple values for a name headers.Add(name, value_Renamed); } } else if (SmtpState.DATA_BODY == response.NextState) { if (bodyLineCount > 0) body.Append(CR); body.Append(commandData); bodyLineCount++; } } }