SmtpStream.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // System.Web.Mail.SmtpStream.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Collections;
  31. using System.Text;
  32. using System.Security.Cryptography;
  33. namespace System.Web.Mail {
  34. internal class SmtpStream {
  35. protected Stream stream;
  36. protected Encoding encoding;
  37. protected SmtpResponse lastResponse;
  38. protected string command = "";
  39. public SmtpStream( Stream stream ) {
  40. this.stream = stream;
  41. encoding = new ASCIIEncoding();
  42. }
  43. public Stream Stream {
  44. get { return stream; }
  45. }
  46. public SmtpResponse LastResponse {
  47. get { return lastResponse; }
  48. }
  49. public void WriteRset() {
  50. command = "RSET";
  51. WriteLine( command );
  52. ReadResponse();
  53. CheckForStatusCode( 250 );
  54. }
  55. public void WriteHelo( string hostName ) {
  56. command = "HELO " + hostName;
  57. WriteLine( command );
  58. ReadResponse();
  59. CheckForStatusCode( 250 );
  60. }
  61. public void WriteMailFrom( string from ) {
  62. command = "MAIL FROM: <" + from + ">";
  63. WriteLine( command );
  64. ReadResponse();
  65. CheckForStatusCode( 250 );
  66. }
  67. public void WriteRcptTo( string to ) {
  68. command = "RCPT TO: <" + to + ">";
  69. WriteLine( command );
  70. ReadResponse();
  71. CheckForStatusCode( 250 );
  72. }
  73. public void WriteData() {
  74. command = "DATA";
  75. WriteLine( command );
  76. ReadResponse();
  77. CheckForStatusCode( 354 );
  78. }
  79. public void WriteQuit() {
  80. command = "QUIT";
  81. WriteLine( command );
  82. ReadResponse();
  83. CheckForStatusCode( 221 );
  84. }
  85. public void WriteBoundary( string boundary ) {
  86. WriteLine( "\r\n--{0}" , boundary );
  87. }
  88. public void WriteFinalBoundary( string boundary ) {
  89. WriteLine( "\r\n--{0}--" , boundary );
  90. }
  91. // single dot by itself
  92. public void WriteDataEndTag() {
  93. command = "\r\n.";
  94. WriteLine( command );
  95. ReadResponse();
  96. CheckForStatusCode( 250 );
  97. }
  98. public void WriteHeader( MailHeader header ) {
  99. // write the headers
  100. foreach( string key in header.Data.AllKeys )
  101. WriteLine( "{0}: {1}" , key , header.Data[ key ] );
  102. // write the header end tag
  103. WriteLine( "" );
  104. }
  105. public void CheckForStatusCode( int statusCode ) {
  106. if( LastResponse.StatusCode != statusCode ) {
  107. string msg = "" +
  108. "Server reponse: '" + lastResponse.RawResponse + "';" +
  109. "Status code: '" + lastResponse.StatusCode + "';" +
  110. "Expected status code: '" + statusCode + "';" +
  111. "Last command: '" + command + "'";
  112. throw new SmtpException( msg );
  113. }
  114. }
  115. // write buffer's bytes to the stream
  116. public void WriteBytes( byte[] buffer ) {
  117. stream.Write( buffer , 0 , buffer.Length );
  118. }
  119. // writes a formatted line to the server
  120. public void WriteLine( string format , params object[] args ) {
  121. WriteLine( String.Format( format , args ) );
  122. }
  123. // writes a line to the server
  124. public void WriteLine( string line ) {
  125. byte[] buffer = encoding.GetBytes( line + "\r\n" );
  126. stream.Write( buffer , 0 , buffer.Length );
  127. #if DEBUG
  128. DebugPrint( line );
  129. #endif
  130. }
  131. // read a line from the server
  132. public void ReadResponse( ) {
  133. string line = null;
  134. byte[] buffer = new byte[ 4096 ];
  135. int readLength = stream.Read( buffer , 0 , buffer.Length );
  136. if( readLength > 0 ) {
  137. line = encoding.GetString( buffer , 0 , readLength );
  138. line = line.TrimEnd( new Char[] { '\r' , '\n' , ' ' } );
  139. }
  140. // parse the line to the lastResponse object
  141. lastResponse = SmtpResponse.Parse( line );
  142. #if DEBUG
  143. DebugPrint( line );
  144. #endif
  145. }
  146. /// debug printing
  147. private void DebugPrint( string line ) {
  148. Console.WriteLine( "smtp: {0}" , line );
  149. }
  150. }
  151. }