SmtpClient.cs 5.9 KB

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