SmtpClient.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // System.Web.Mail.SmtpClient.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. // Sanjay Gupta <[email protected]>
  7. // (C) 2004, Novell, Inc. (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Net;
  31. using System.IO;
  32. using System.Text;
  33. using System.Collections;
  34. using System.Net.Sockets;
  35. namespace System.Web.Mail {
  36. /// represents a conntection to a smtp server
  37. internal class SmtpClient {
  38. private string server;
  39. private TcpClient tcpConnection;
  40. private SmtpStream smtp;
  41. //Initialise the variables and connect
  42. public SmtpClient( string server ) {
  43. this.server = server;
  44. Connect();
  45. }
  46. // make the actual connection
  47. // and HELO handshaking
  48. private void Connect() {
  49. tcpConnection = new TcpClient( server , 25 );
  50. Stream stream = tcpConnection.GetStream();
  51. smtp = new SmtpStream( stream );
  52. // read the server greeting
  53. smtp.ReadResponse();
  54. smtp.CheckForStatusCode( 220 );
  55. // write the HELO command to the server
  56. smtp.WriteHelo( Dns.GetHostName() );
  57. }
  58. public void Send( MailMessageWrapper msg ) {
  59. if( msg.From == null ) {
  60. throw new SmtpException( "From property must be set." );
  61. }
  62. if( msg.To == null ) {
  63. if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
  64. }
  65. // start with a reset incase old data
  66. // is present at the server in this session
  67. smtp.WriteRset();
  68. // write the mail from command
  69. smtp.WriteMailFrom( msg.From.Address );
  70. // write the rcpt to command for the To addresses
  71. foreach( MailAddress addr in msg.To ) {
  72. smtp.WriteRcptTo( addr.Address );
  73. }
  74. // write the rcpt to command for the Cc addresses
  75. foreach( MailAddress addr in msg.Cc ) {
  76. smtp.WriteRcptTo( addr.Address );
  77. }
  78. // write the rcpt to command for the Bcc addresses
  79. foreach( MailAddress addr in msg.Bcc ) {
  80. smtp.WriteRcptTo( addr.Address );
  81. }
  82. // write the data command and then
  83. // send the email
  84. smtp.WriteData();
  85. if( msg.Attachments.Count == 0 ) {
  86. SendSinglepartMail( msg );
  87. } else {
  88. SendMultipartMail( msg );
  89. }
  90. // write the data end tag "."
  91. smtp.WriteDataEndTag();
  92. }
  93. // sends a single part mail to the server
  94. private void SendSinglepartMail( MailMessageWrapper msg ) {
  95. // write the header
  96. smtp.WriteHeader( msg.Header );
  97. // send the mail body
  98. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  99. }
  100. // sends a multipart mail to the server
  101. private void SendMultipartMail( MailMessageWrapper msg ) {
  102. // generate the boundary between attachments
  103. string boundary = MailUtil.GenerateBoundary();
  104. // set the Content-Type header to multipart/mixed
  105. string bodyContentType = msg.Header.ContentType;
  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 = bodyContentType;
  115. #if NET_1_1
  116. // Add all the custom headers to body part as specified in
  117. //Fields property of MailMessageWrapper
  118. //Remove fields specific for authenticating to SMTP server.
  119. //Need to incorporate AUTH command in SmtpStream to handle
  120. //Authorization info. Its a temporary fix for Bug no 68829.
  121. //Will dig some more on SMTP AUTH command, and then implement
  122. //Authorization. - Sanjay
  123. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
  124. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
  125. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
  126. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendusername");
  127. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
  128. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendpassword");
  129. partHeader.Data.Add (msg.Fields.Data);
  130. #endif
  131. smtp.WriteHeader( partHeader );
  132. // FIXME: probably need to use QP or Base64 on everything higher
  133. // then 8-bit .. like utf-16
  134. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  135. smtp.WriteBoundary( boundary );
  136. // now start to write the attachments
  137. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  138. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  139. FileInfo fileInfo = new FileInfo( a.Filename );
  140. MailHeader aHeader = new MailHeader();
  141. aHeader.ContentType =
  142. String.Format (MimeTypes.GetMimeType (fileInfo.Name) + "; name=\"{0}\"",fileInfo.Name);
  143. aHeader.ContentDisposition =
  144. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  145. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  146. smtp.WriteHeader( aHeader );
  147. // perform the actual writing of the file.
  148. // read from the file stream and write to the tcp stream
  149. FileStream ins = new FileStream( fileInfo.FullName , FileMode.Open );
  150. // create an apropriate encoder
  151. IAttachmentEncoder encoder;
  152. if( a.Encoding == MailEncoding.UUEncode ) {
  153. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  154. } else {
  155. encoder = new Base64AttachmentEncoder();
  156. }
  157. encoder.EncodeStream( ins , smtp.Stream );
  158. ins.Close();
  159. smtp.WriteLine( "" );
  160. // if it is the last attachment write
  161. // the final boundary otherwise write
  162. // a normal one.
  163. if( i < (msg.Attachments.Count - 1) ) {
  164. smtp.WriteBoundary( boundary );
  165. } else {
  166. smtp.WriteFinalBoundary( boundary );
  167. }
  168. }
  169. }
  170. // send quit command and
  171. // closes the connection
  172. public void Close() {
  173. smtp.WriteQuit();
  174. tcpConnection.Close();
  175. }
  176. }
  177. }