SmtpStream.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Web.Mail.SmtpStream.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Text;
  12. using System.Security.Cryptography;
  13. namespace System.Web.Mail {
  14. internal class SmtpStream {
  15. protected Stream stream;
  16. protected Encoding encoding;
  17. protected SmtpResponse lastResponse;
  18. protected string command = "";
  19. public SmtpStream( Stream stream ) {
  20. this.stream = stream;
  21. encoding = new ASCIIEncoding();
  22. }
  23. public Stream Stream {
  24. get { return stream; }
  25. }
  26. public SmtpResponse LastResponse {
  27. get { return lastResponse; }
  28. }
  29. public void WriteRset() {
  30. command = "RSET";
  31. WriteLine( command );
  32. ReadResponse();
  33. CheckForStatusCode( 250 );
  34. }
  35. public void WriteHelo( string hostName ) {
  36. command = "HELO " + hostName;
  37. WriteLine( command );
  38. ReadResponse();
  39. CheckForStatusCode( 250 );
  40. }
  41. public void WriteMailFrom( string from ) {
  42. command = "MAIL FROM: <" + from + ">";
  43. WriteLine( command );
  44. ReadResponse();
  45. CheckForStatusCode( 250 );
  46. }
  47. public void WriteRcptTo( string to ) {
  48. command = "RCPT TO: <" + to + ">";
  49. WriteLine( command );
  50. ReadResponse();
  51. CheckForStatusCode( 250 );
  52. }
  53. public void WriteData() {
  54. command = "DATA";
  55. WriteLine( command );
  56. ReadResponse();
  57. CheckForStatusCode( 354 );
  58. }
  59. public void WriteQuit() {
  60. command = "QUIT";
  61. WriteLine( command );
  62. ReadResponse();
  63. CheckForStatusCode( 221 );
  64. }
  65. public void WriteBoundary( string boundary ) {
  66. WriteLine( "\r\n--{0}" , boundary );
  67. }
  68. public void WriteFinalBoundary( string boundary ) {
  69. WriteLine( "\r\n--{0}--" , boundary );
  70. }
  71. // single dot by itself
  72. public void WriteDataEndTag() {
  73. command = "\r\n.";
  74. WriteLine( command );
  75. ReadResponse();
  76. CheckForStatusCode( 250 );
  77. }
  78. public void WriteHeader( MailHeader header ) {
  79. // write the headers
  80. foreach( string key in header.Data.AllKeys )
  81. WriteLine( "{0}: {1}" , key , header.Data[ key ] );
  82. // write the header end tag
  83. WriteLine( "" );
  84. }
  85. public void CheckForStatusCode( int statusCode ) {
  86. if( LastResponse.StatusCode != statusCode ) {
  87. string msg = "" +
  88. "Server reponse: '" + lastResponse.RawResponse + "';" +
  89. "Status code: '" + lastResponse.StatusCode + "';" +
  90. "Expected status code: '" + statusCode + "';" +
  91. "Last command: '" + command + "'";
  92. throw new SmtpException( msg );
  93. }
  94. }
  95. // write buffer's bytes to the stream
  96. public void WriteBytes( byte[] buffer ) {
  97. stream.Write( buffer , 0 , buffer.Length );
  98. }
  99. // writes a formatted line to the server
  100. public void WriteLine( string format , params object[] args ) {
  101. WriteLine( String.Format( format , args ) );
  102. }
  103. // writes a line to the server
  104. public void WriteLine( string line ) {
  105. byte[] buffer = encoding.GetBytes( line + "\r\n" );
  106. stream.Write( buffer , 0 , buffer.Length );
  107. #if DEBUG
  108. DebugPrint( line );
  109. #endif
  110. }
  111. // read a line from the server
  112. public void ReadResponse( ) {
  113. string line = null;
  114. byte[] buffer = new byte[ 4096 ];
  115. int readLength = stream.Read( buffer , 0 , buffer.Length );
  116. if( readLength > 0 ) {
  117. line = encoding.GetString( buffer , 0 , readLength );
  118. line = line.TrimEnd( new Char[] { '\r' , '\n' , ' ' } );
  119. }
  120. // parse the line to the lastResponse object
  121. lastResponse = SmtpResponse.Parse( line );
  122. #if DEBUG
  123. DebugPrint( line );
  124. #endif
  125. }
  126. /// debug printing
  127. private void DebugPrint( string line ) {
  128. Console.WriteLine( "smtp: {0}" , line );
  129. }
  130. }
  131. }