SmtpClient.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // System.Web.Mail.SmtpClient.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.Net;
  10. using System.IO;
  11. using System.Text;
  12. using System.Collections;
  13. using System.Net.Sockets;
  14. namespace System.Web.Mail {
  15. /// represents a conntection to a smtp server
  16. internal class SmtpClient {
  17. private string server;
  18. private TcpClient tcpConnection;
  19. private SmtpStream smtp;
  20. private Encoding encoding;
  21. //Initialise the variables and connect
  22. public SmtpClient( string server ) {
  23. this.server = server;
  24. encoding = new ASCIIEncoding( );
  25. Connect();
  26. }
  27. // make the actual connection
  28. // and HELO handshaking
  29. private void Connect() {
  30. tcpConnection = new TcpClient( server , 25 );
  31. Stream stream = tcpConnection.GetStream();
  32. smtp = new SmtpStream( stream );
  33. // read the server greeting
  34. smtp.ReadResponse();
  35. smtp.CheckForStatusCode( 220 );
  36. // write the HELO command to the server
  37. smtp.WriteHelo( Dns.GetHostName() );
  38. }
  39. public void Send( MailMessageWrapper msg ) {
  40. if( msg.From == null ) {
  41. throw new SmtpException( "From property must be set." );
  42. }
  43. if( msg.To == null ) {
  44. if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
  45. }
  46. // start with a reset incase old data
  47. // is present at the server in this session
  48. smtp.WriteRset();
  49. // write the mail from command
  50. smtp.WriteMailFrom( msg.From.Address );
  51. // write the rcpt to command for the To addresses
  52. foreach( MailAddress addr in msg.To ) {
  53. smtp.WriteRcptTo( addr.Address );
  54. }
  55. // write the rcpt to command for the Cc addresses
  56. foreach( MailAddress addr in msg.Cc ) {
  57. smtp.WriteRcptTo( addr.Address );
  58. }
  59. // write the data command and then
  60. // send the email
  61. smtp.WriteData();
  62. if( msg.Attachments.Count == 0 ) {
  63. SendSinglepartMail( msg );
  64. } else {
  65. SendMultipartMail( msg );
  66. }
  67. // write the data end tag "."
  68. smtp.WriteDataEndTag();
  69. }
  70. // sends a single part mail to the server
  71. private void SendSinglepartMail( MailMessageWrapper msg ) {
  72. // write the header
  73. smtp.WriteHeader( msg.Header );
  74. // send the mail body
  75. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  76. }
  77. // sends a multipart mail to the server
  78. private void SendMultipartMail( MailMessageWrapper msg ) {
  79. // set the part boundary FIXME: THIS SHOULD NOT BE HARDCODED
  80. // look att Gaurav Vaish implementation
  81. string boundary = "NextPart_000_1113_1962_1fe8";
  82. // set the Content-Type header to multipart/mixed
  83. msg.Header.ContentType =
  84. String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
  85. // write the header
  86. smtp.WriteHeader( msg.Header );
  87. // write the first part text part
  88. // before the attachments
  89. smtp.WriteBoundary( boundary );
  90. MailHeader partHeader = new MailHeader();
  91. partHeader.ContentType = "text/plain";
  92. smtp.WriteHeader( partHeader );
  93. // FIXME: probably need to use QP or Base64 on everything higher
  94. // then 8-bit .. like utf-16
  95. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  96. smtp.WriteBoundary( boundary );
  97. // now start to write the attachments
  98. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  99. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  100. FileInfo fileInfo = new FileInfo( a.Filename );
  101. MailHeader aHeader = new MailHeader();
  102. aHeader.ContentType =
  103. String.Format( "application/octet-stream; name=\"{0}\"",
  104. fileInfo.Name );
  105. aHeader.ContentDisposition =
  106. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  107. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  108. smtp.WriteHeader( aHeader );
  109. // perform the actual writing of the file.
  110. // read from the file stream and write to the tcp stream
  111. FileStream ins = new FileStream( fileInfo.FullName , FileMode.Open );
  112. // create an apropriate encoder
  113. IAttachmentEncoder encoder;
  114. if( a.Encoding == MailEncoding.UUEncode ) {
  115. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  116. } else {
  117. encoder = new Base64AttachmentEncoder();
  118. }
  119. encoder.EncodeStream( ins , smtp.Stream );
  120. ins.Close();
  121. smtp.WriteLine( "" );
  122. // if it is the last attachment write
  123. // the final boundary otherwise write
  124. // a normal one.
  125. if( i < (msg.Attachments.Count - 1) ) {
  126. smtp.WriteBoundary( boundary );
  127. } else {
  128. smtp.WriteFinalBoundary( boundary );
  129. }
  130. }
  131. }
  132. // send quit command and
  133. // closes the connection
  134. public void Close() {
  135. smtp.WriteQuit();
  136. tcpConnection.Close();
  137. }
  138. }
  139. }