SmtpStream.cs 5.6 KB

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