SmtpStream.cs 3.7 KB

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