SmtpClient.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.Net;
  30. using System.IO;
  31. using System.Text;
  32. using System.Collections;
  33. using System.Net.Sockets;
  34. using System.Security.Permissions;
  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. // SECURITY-FIXME: lower assertion with imperative asserts
  101. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  102. // sends a multipart mail to the server
  103. private void SendMultipartMail( MailMessageWrapper msg ) {
  104. // generate the boundary between attachments
  105. string boundary = MailUtil.GenerateBoundary();
  106. // set the Content-Type header to multipart/mixed
  107. string bodyContentType = msg.Header.ContentType;
  108. msg.Header.ContentType =
  109. String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
  110. // write the header
  111. smtp.WriteHeader( msg.Header );
  112. // write the first part text part
  113. // before the attachments
  114. smtp.WriteBoundary( boundary );
  115. MailHeader partHeader = new MailHeader();
  116. partHeader.ContentType = bodyContentType;
  117. #if NET_1_1
  118. // Add all the custom headers to body part as specified in
  119. //Fields property of MailMessageWrapper
  120. //Remove fields specific for authenticating to SMTP server.
  121. //Need to incorporate AUTH command in SmtpStream to handle
  122. //Authorization info. Its a temporary fix for Bug no 68829.
  123. //Will dig some more on SMTP AUTH command, and then implement
  124. //Authorization. - Sanjay
  125. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
  126. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
  127. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
  128. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendusername");
  129. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
  130. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendpassword");
  131. partHeader.Data.Add (msg.Fields.Data);
  132. #endif
  133. smtp.WriteHeader( partHeader );
  134. // FIXME: probably need to use QP or Base64 on everything higher
  135. // then 8-bit .. like utf-16
  136. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  137. smtp.WriteBoundary( boundary );
  138. // now start to write the attachments
  139. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  140. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  141. FileInfo fileInfo = new FileInfo( a.Filename );
  142. MailHeader aHeader = new MailHeader();
  143. aHeader.ContentType =
  144. String.Format (MimeTypes.GetMimeType (fileInfo.Name) + "; name=\"{0}\"",fileInfo.Name);
  145. aHeader.ContentDisposition =
  146. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  147. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  148. smtp.WriteHeader( aHeader );
  149. // perform the actual writing of the file.
  150. // read from the file stream and write to the tcp stream
  151. FileStream ins = fileInfo.OpenRead ();
  152. // create an apropriate encoder
  153. IAttachmentEncoder encoder;
  154. if( a.Encoding == MailEncoding.UUEncode ) {
  155. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  156. } else {
  157. encoder = new Base64AttachmentEncoder();
  158. }
  159. encoder.EncodeStream( ins , smtp.Stream );
  160. ins.Close();
  161. smtp.WriteLine( "" );
  162. // if it is the last attachment write
  163. // the final boundary otherwise write
  164. // a normal one.
  165. if( i < (msg.Attachments.Count - 1) ) {
  166. smtp.WriteBoundary( boundary );
  167. } else {
  168. smtp.WriteFinalBoundary( boundary );
  169. }
  170. }
  171. }
  172. // send quit command and
  173. // closes the connection
  174. public void Close() {
  175. smtp.WriteQuit();
  176. tcpConnection.Close();
  177. }
  178. }
  179. }