SmtpClient.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. private Encoding encoding;
  42. //Initialise the variables and connect
  43. public SmtpClient( string server ) {
  44. this.server = server;
  45. encoding = new ASCIIEncoding( );
  46. Connect();
  47. }
  48. // make the actual connection
  49. // and HELO handshaking
  50. private void Connect() {
  51. tcpConnection = new TcpClient( server , 25 );
  52. Stream stream = tcpConnection.GetStream();
  53. smtp = new SmtpStream( stream );
  54. // read the server greeting
  55. smtp.ReadResponse();
  56. smtp.CheckForStatusCode( 220 );
  57. // write the HELO command to the server
  58. smtp.WriteHelo( Dns.GetHostName() );
  59. }
  60. public void Send( MailMessageWrapper msg ) {
  61. if( msg.From == null ) {
  62. throw new SmtpException( "From property must be set." );
  63. }
  64. if( msg.To == null ) {
  65. if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
  66. }
  67. // start with a reset incase old data
  68. // is present at the server in this session
  69. smtp.WriteRset();
  70. // write the mail from command
  71. smtp.WriteMailFrom( msg.From.Address );
  72. // write the rcpt to command for the To addresses
  73. foreach( MailAddress addr in msg.To ) {
  74. smtp.WriteRcptTo( addr.Address );
  75. }
  76. // write the rcpt to command for the Cc addresses
  77. foreach( MailAddress addr in msg.Cc ) {
  78. smtp.WriteRcptTo( addr.Address );
  79. }
  80. // write the rcpt to command for the Bcc addresses
  81. foreach( MailAddress addr in msg.Bcc ) {
  82. smtp.WriteRcptTo( addr.Address );
  83. }
  84. // write the data command and then
  85. // send the email
  86. smtp.WriteData();
  87. if( msg.Attachments.Count == 0 ) {
  88. #if NET_2_0
  89. //The message might be multipart, if RelatedBodyParts are present
  90. if (msg.RelatedBodyParts.Count != 0)
  91. SendMultipartMail (msg);
  92. else
  93. #endif
  94. SendSinglepartMail( msg );
  95. } else {
  96. SendMultipartMail( msg );
  97. }
  98. // write the data end tag "."
  99. smtp.WriteDataEndTag();
  100. }
  101. // sends a single part mail to the server
  102. private void SendSinglepartMail( MailMessageWrapper msg ) {
  103. // write the header
  104. smtp.WriteHeader( msg.Header );
  105. // send the mail body
  106. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  107. }
  108. // sends a multipart mail to the server
  109. private void SendMultipartMail( MailMessageWrapper msg ) {
  110. // generate the boundary between attachments
  111. string boundary = MailUtil.GenerateBoundary();
  112. // set the Content-Type header to multipart/mixed
  113. string bodyContentType = msg.Header.ContentType;
  114. #if NET_2_0
  115. if (msg.RelatedBodyParts.Count != 0)
  116. msg.Header.ContentType = String.Format( "multipart/related;\r\n boundary={0}" , boundary );
  117. else
  118. #endif
  119. msg.Header.ContentType =
  120. String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
  121. // write the header
  122. smtp.WriteHeader( msg.Header );
  123. // write the first part text part
  124. // before the attachments
  125. smtp.WriteBoundary( boundary );
  126. MailHeader partHeader = new MailHeader();
  127. partHeader.ContentType = bodyContentType;
  128. #if NET_1_1
  129. // Add all the custom headers to body part as specified in
  130. //Fields property of MailMessageWrapper
  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. #if NET_2_0
  139. for (int i = 0; i < msg.RelatedBodyParts.Count; i++) {
  140. RelatedBodyPart rbp = (RelatedBodyPart) msg.RelatedBodyParts [i];
  141. FileInfo file = new FileInfo (rbp.Path);
  142. MailHeader header = new MailHeader ();
  143. header.ContentLocation = rbp.Path;
  144. header.ContentType = String.Format (MimeTypes.GetMimeType (file.Name) + "; name=\"{0}\"",file.Name);
  145. //If content id and ContentLocation both are present
  146. //in mime header of a mail, than RelatedBodyPart of mail
  147. //doesnt show up in a machine other than from which mail
  148. //was sent, and hence only one of them is inserted in
  149. //header. Need to check how the things go when another
  150. //body part refers the content with the content id specified
  151. /*if (rbp.Name != null)
  152. header.Data.Add ("Content-ID", "<"+rbp.Name+">");*/
  153. header.ContentTransferEncoding = "Base64";
  154. header.ContentDisposition = String.Format( "inline; filename=\"{0}\"" , file.Name );
  155. smtp.WriteHeader (header);
  156. FileStream rbpStream = new FileStream (file.FullName, FileMode.Open);
  157. IAttachmentEncoder rbpEncoder = new Base64AttachmentEncoder ();
  158. rbpEncoder.EncodeStream (rbpStream, smtp.Stream);
  159. rbpStream.Close();
  160. smtp.WriteLine( "" );
  161. if (i < (msg.RelatedBodyParts.Count - 1)) {
  162. smtp.WriteBoundary (boundary);
  163. } else {
  164. if (msg.Attachments.Count == 0)
  165. smtp.WriteFinalBoundary (boundary);
  166. else
  167. smtp.WriteBoundary (boundary);
  168. }
  169. }
  170. #endif
  171. // now start to write the attachments
  172. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  173. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  174. FileInfo fileInfo = new FileInfo( a.Filename );
  175. MailHeader aHeader = new MailHeader();
  176. aHeader.ContentType =
  177. String.Format (MimeTypes.GetMimeType (fileInfo.Name) + "; name=\"{0}\"",fileInfo.Name);
  178. aHeader.ContentDisposition =
  179. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  180. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  181. smtp.WriteHeader( aHeader );
  182. // perform the actual writing of the file.
  183. // read from the file stream and write to the tcp stream
  184. FileStream ins = new FileStream( fileInfo.FullName , FileMode.Open );
  185. // create an apropriate encoder
  186. IAttachmentEncoder encoder;
  187. if( a.Encoding == MailEncoding.UUEncode ) {
  188. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  189. } else {
  190. encoder = new Base64AttachmentEncoder();
  191. }
  192. encoder.EncodeStream( ins , smtp.Stream );
  193. ins.Close();
  194. smtp.WriteLine( "" );
  195. // if it is the last attachment write
  196. // the final boundary otherwise write
  197. // a normal one.
  198. if( i < (msg.Attachments.Count - 1) ) {
  199. smtp.WriteBoundary( boundary );
  200. } else {
  201. smtp.WriteFinalBoundary( boundary );
  202. }
  203. }
  204. }
  205. // send quit command and
  206. // closes the connection
  207. public void Close() {
  208. smtp.WriteQuit();
  209. tcpConnection.Close();
  210. }
  211. }
  212. }