SmtpStream.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 WriteAuthLogin()
  56. {
  57. command = "AUTH LOGIN";
  58. WriteLine( command );
  59. ReadResponse();
  60. }
  61. public bool WriteStartTLS( )
  62. {
  63. command = "STARTTLS";
  64. WriteLine( command );
  65. ReadResponse();
  66. return LastResponse.StatusCode == 220;
  67. }
  68. public void WriteEhlo( string hostName )
  69. {
  70. command = "EHLO " + hostName;
  71. WriteLine( command );
  72. ReadResponse();
  73. CheckForStatusCode( 250 );
  74. }
  75. public void WriteHelo( string hostName ) {
  76. command = "HELO " + hostName;
  77. WriteLine( command );
  78. ReadResponse();
  79. CheckForStatusCode( 250 );
  80. }
  81. public void WriteMailFrom( string from ) {
  82. command = "MAIL FROM: <" + from + ">";
  83. WriteLine( command );
  84. ReadResponse();
  85. CheckForStatusCode( 250 );
  86. }
  87. public void WriteRcptTo( string to ) {
  88. command = "RCPT TO: <" + to + ">";
  89. WriteLine( command );
  90. ReadResponse();
  91. CheckForStatusCode( 250 );
  92. }
  93. public void WriteData() {
  94. command = "DATA";
  95. WriteLine( command );
  96. ReadResponse();
  97. CheckForStatusCode( 354 );
  98. }
  99. public void WriteQuit() {
  100. command = "QUIT";
  101. WriteLine( command );
  102. ReadResponse();
  103. CheckForStatusCode( 221 );
  104. }
  105. public void WriteBoundary( string boundary ) {
  106. WriteLine( "\r\n--{0}" , boundary );
  107. }
  108. public void WriteFinalBoundary( string boundary ) {
  109. WriteLine( "\r\n--{0}--" , boundary );
  110. }
  111. // single dot by itself
  112. public void WriteDataEndTag() {
  113. command = "\r\n.";
  114. WriteLine( command );
  115. ReadResponse();
  116. CheckForStatusCode( 250 );
  117. }
  118. public void WriteHeader( MailHeader header ) {
  119. // write the headers
  120. foreach( string key in header.Data.AllKeys )
  121. WriteLine( "{0}: {1}" , key , header.Data[ key ] );
  122. // write the header end tag
  123. WriteLine( "" );
  124. }
  125. public void CheckForStatusCode( int statusCode ) {
  126. if( LastResponse.StatusCode != statusCode ) {
  127. string msg = "" +
  128. "Server reponse: '" + lastResponse.RawResponse + "';" +
  129. "Status code: '" + lastResponse.StatusCode + "';" +
  130. "Expected status code: '" + statusCode + "';" +
  131. "Last command: '" + command + "'";
  132. throw new SmtpException( msg );
  133. }
  134. }
  135. // write buffer's bytes to the stream
  136. public void WriteBytes( byte[] buffer ) {
  137. stream.Write( buffer , 0 , buffer.Length );
  138. }
  139. // writes a formatted line to the server
  140. public void WriteLine( string format , params object[] args ) {
  141. WriteLine( String.Format( format , args ) );
  142. }
  143. // writes a line to the server
  144. public void WriteLine( string line ) {
  145. byte[] buffer = encoding.GetBytes( line + "\r\n" );
  146. stream.Write( buffer , 0 , buffer.Length );
  147. #if DEBUG
  148. DebugPrint( line );
  149. #endif
  150. }
  151. // read a line from the server
  152. public void ReadResponse( ) {
  153. byte[] buffer = new byte [512];
  154. int position = 0;
  155. bool lastLine = false;
  156. do {
  157. int readLength = stream.Read (buffer , position , buffer.Length - position);
  158. if (readLength > 0) {
  159. int available = position + readLength - 1;
  160. if (available > 4 && (buffer [available] == '\n' || buffer [available] == '\r'))
  161. for (int index = available - 3; ; index--) {
  162. if (index < 0 || buffer [index] == '\n' || buffer [index] == '\r') {
  163. lastLine = buffer [index + 4] == ' ';
  164. break;
  165. }
  166. }
  167. // move position
  168. position += readLength;
  169. // check if buffer is full
  170. if (position == buffer.Length) {
  171. byte [] newBuffer = new byte [buffer.Length * 2];
  172. Array.Copy (buffer, 0, newBuffer, 0, buffer.Length);
  173. buffer = newBuffer;
  174. }
  175. }
  176. } while(!lastLine);
  177. string line = encoding.GetString (buffer , 0 , position - 1);
  178. // parse the line to the lastResponse object
  179. lastResponse = SmtpResponse.Parse (line);
  180. #if DEBUG
  181. DebugPrint( line );
  182. #endif
  183. }
  184. #if DEBUG
  185. /// debug printing
  186. private void DebugPrint( string line ) {
  187. Console.WriteLine( "smtp: {0}" , line );
  188. }
  189. #endif
  190. }
  191. }