SmtpClient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 rcpt to command for the Bcc addresses
  60. foreach( MailAddress addr in msg.Bcc ) {
  61. smtp.WriteRcptTo( addr.Address );
  62. }
  63. // write the data command and then
  64. // send the email
  65. smtp.WriteData();
  66. if( msg.Attachments.Count == 0 ) {
  67. SendSinglepartMail( msg );
  68. } else {
  69. SendMultipartMail( msg );
  70. }
  71. // write the data end tag "."
  72. smtp.WriteDataEndTag();
  73. }
  74. // sends a single part mail to the server
  75. private void SendSinglepartMail( MailMessageWrapper msg ) {
  76. // write the header
  77. smtp.WriteHeader( msg.Header );
  78. // send the mail body
  79. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  80. }
  81. // sends a multipart mail to the server
  82. private void SendMultipartMail( MailMessageWrapper msg ) {
  83. // generate the boundary between attachments
  84. string boundary = MailUtil.GenerateBoundary();
  85. // set the Content-Type header to multipart/mixed
  86. msg.Header.ContentType =
  87. String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
  88. // write the header
  89. smtp.WriteHeader( msg.Header );
  90. // write the first part text part
  91. // before the attachments
  92. smtp.WriteBoundary( boundary );
  93. MailHeader partHeader = new MailHeader();
  94. partHeader.ContentType = "text/plain";
  95. smtp.WriteHeader( partHeader );
  96. // FIXME: probably need to use QP or Base64 on everything higher
  97. // then 8-bit .. like utf-16
  98. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  99. smtp.WriteBoundary( boundary );
  100. // now start to write the attachments
  101. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  102. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  103. FileInfo fileInfo = new FileInfo( a.Filename );
  104. MailHeader aHeader = new MailHeader();
  105. aHeader.ContentType =
  106. String.Format( "application/octet-stream; name=\"{0}\"",
  107. fileInfo.Name );
  108. aHeader.ContentDisposition =
  109. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  110. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  111. smtp.WriteHeader( aHeader );
  112. // perform the actual writing of the file.
  113. // read from the file stream and write to the tcp stream
  114. FileStream ins = new FileStream( fileInfo.FullName , FileMode.Open );
  115. // create an apropriate encoder
  116. IAttachmentEncoder encoder;
  117. if( a.Encoding == MailEncoding.UUEncode ) {
  118. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  119. } else {
  120. encoder = new Base64AttachmentEncoder();
  121. }
  122. encoder.EncodeStream( ins , smtp.Stream );
  123. ins.Close();
  124. smtp.WriteLine( "" );
  125. // if it is the last attachment write
  126. // the final boundary otherwise write
  127. // a normal one.
  128. if( i < (msg.Attachments.Count - 1) ) {
  129. smtp.WriteBoundary( boundary );
  130. } else {
  131. smtp.WriteFinalBoundary( boundary );
  132. }
  133. }
  134. }
  135. // send quit command and
  136. // closes the connection
  137. public void Close() {
  138. smtp.WriteQuit();
  139. tcpConnection.Close();
  140. }
  141. }
  142. }