/// parses a new response object from a response string public static SmtpResponse Parse(string line) { SmtpResponse response = new SmtpResponse(); if (line.Length < 4) { throw new SmtpException("Response is to short " + line.Length + "."); } if ((line[3] != ' ') && (line[3] != '-')) { throw new SmtpException("Response format is wrong.(" + line + ")"); } // parse the response code response.StatusCode = Int32.Parse(line.Substring(0, 3)); // set the rawsponse response.RawResponse = line; // set the response parts response.Parts = line.Substring(0, 3).Split(';'); return(response); }
// read a line from the server public void ReadResponse( ) { byte[] buffer = new byte [512]; int position = 0; bool lastLine = false; do { int readLength = stream.Read(buffer, position, buffer.Length - position); if (readLength > 0) { int available = position + readLength - 1; if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r')) { for (int index = available - 3; ; index--) { if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') { lastLine = buffer [index + 4] == ' '; break; } } } // move position position += readLength; // check if buffer is full if (position == buffer.Length) { byte [] newBuffer = new byte [buffer.Length * 2]; Array.Copy(buffer, 0, newBuffer, 0, buffer.Length); buffer = newBuffer; } } } while(!lastLine); string line = encoding.GetString(buffer, 0, position - 1); // parse the line to the lastResponse object lastResponse = SmtpResponse.Parse(line); Debug.WriteLine("smtp: {0}", line); }
/// parses a new response object from a response string public static SmtpResponse Parse( string line ) { SmtpResponse response = new SmtpResponse(); if( line.Length < 4 ) throw new SmtpException( "Response is to short " + line.Length + "."); if( ( line[ 3 ] != ' ' ) && ( line[ 3 ] != '-' ) ) throw new SmtpException( "Response format is wrong.(" + line + ")" ); // parse the response code response.StatusCode = Int32.Parse( line.Substring( 0 , 3 ) ); // set the rawsponse response.RawResponse = line; // set the response parts response.Parts = line.Substring( 0 , 3 ).Split( ';' ); return response; }
private static bool CheckResponse( Socket socket, SmtpResponse response ) { int byteCount = socket.Receive( m_Buffer, 0, m_Buffer.Length, SocketFlags.None ); if ( byteCount <= 2 ) { Shutdown( socket ); return false; } int resp = (int)response; byte dig1 = (byte)( '0' + ((resp / 100) % 10) ); byte dig2 = (byte)( '0' + ((resp / 10) % 10) ); byte dig3 = (byte)( '0' + ((resp / 1) % 10) ); bool valid = ( m_Buffer[0] == dig1 && m_Buffer[1] == dig2 && m_Buffer[2] == dig3 ); if ( !valid ) Shutdown( socket ); return valid; }
// read a line from the server public void ReadResponse( ) { byte[] buffer = new byte [512]; int position = 0; bool lastLine = false; do { int readLength = stream.Read (buffer , position , buffer.Length - position); if (readLength > 0) { int available = position + readLength - 1; if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r')) for (int index = available - 3; ; index--) { if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') { lastLine = buffer [index + 4] == ' '; break; } } // move position position += readLength; // check if buffer is full if (position == buffer.Length) { byte [] newBuffer = new byte [buffer.Length * 2]; Array.Copy (buffer, 0, newBuffer, 0, buffer.Length); buffer = newBuffer; } } } while(!lastLine); string line = encoding.GetString (buffer , 0 , position - 1); // parse the line to the lastResponse object lastResponse = SmtpResponse.Parse (line); Debug.WriteLine ("smtp: {0}", line); }
private bool CheckResponse(SmtpResponse expected) { string sResponse = null; int response = 0; byte[] bytes = new byte[1024]; while (socket.Available == 0) { System.Threading.Thread.Sleep(100); } socket.Receive(bytes, 0, socket.Available, SocketFlags.None); sResponse = Encoding.ASCII.GetString(bytes); response = Convert.ToInt32(sResponse.Substring(0, 3)); if (response != (int)expected) { return false; } return true; }
// read a line from the server public void ReadResponse( ) { string line = null; byte[] buffer = new byte[ 4096 ]; int readLength = stream.Read( buffer , 0 , buffer.Length ); if( readLength > 0 ) { line = encoding.GetString( buffer , 0 , readLength ); line = line.TrimEnd( new Char[] { '\r' , '\n' , ' ' } ); } // parse the line to the lastResponse object lastResponse = SmtpResponse.Parse( line ); #if DEBUG DebugPrint( line ); #endif }