SmtpClient.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //Remove fields specific for authenticating to SMTP server.
  132. //Need to incorporate AUTH command in SmtpStream to handle
  133. //Authorization info. Its a temporary fix for Bug no 68829.
  134. //Will dig some more on SMTP AUTH command, and then implement
  135. //Authorization. - Sanjay
  136. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
  137. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
  138. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
  139. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendusername");
  140. if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
  141. msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendpassword");
  142. partHeader.Data.Add (msg.Fields.Data);
  143. #endif
  144. smtp.WriteHeader( partHeader );
  145. // FIXME: probably need to use QP or Base64 on everything higher
  146. // then 8-bit .. like utf-16
  147. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  148. smtp.WriteBoundary( boundary );
  149. #if NET_2_0
  150. for (int i = 0; i < msg.RelatedBodyParts.Count; i++) {
  151. RelatedBodyPart rbp = (RelatedBodyPart) msg.RelatedBodyParts [i];
  152. FileInfo file = new FileInfo (rbp.Path);
  153. MailHeader header = new MailHeader ();
  154. header.ContentLocation = rbp.Path;
  155. header.ContentType = String.Format (MimeTypes.GetMimeType (file.Name) + "; name=\"{0}\"",file.Name);
  156. //If content id and ContentLocation both are present
  157. //in mime header of a mail, than RelatedBodyPart of mail
  158. //doesnt show up in a machine other than from which mail
  159. //was sent, and hence only one of them is inserted in
  160. //header. Need to check how the things go when another
  161. //body part refers the content with the content id specified
  162. /*if (rbp.Name != null)
  163. header.Data.Add ("Content-ID", "<"+rbp.Name+">");*/
  164. header.ContentTransferEncoding = "Base64";
  165. header.ContentDisposition = String.Format( "inline; filename=\"{0}\"" , file.Name );
  166. smtp.WriteHeader (header);
  167. FileStream rbpStream = new FileStream (file.FullName, FileMode.Open);
  168. IAttachmentEncoder rbpEncoder = new Base64AttachmentEncoder ();
  169. rbpEncoder.EncodeStream (rbpStream, smtp.Stream);
  170. rbpStream.Close();
  171. smtp.WriteLine( "" );
  172. if (i < (msg.RelatedBodyParts.Count - 1)) {
  173. smtp.WriteBoundary (boundary);
  174. } else {
  175. if (msg.Attachments.Count == 0)
  176. smtp.WriteFinalBoundary (boundary);
  177. else
  178. smtp.WriteBoundary (boundary);
  179. }
  180. }
  181. #endif
  182. // now start to write the attachments
  183. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  184. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  185. FileInfo fileInfo = new FileInfo( a.Filename );
  186. MailHeader aHeader = new MailHeader();
  187. aHeader.ContentType =
  188. String.Format (MimeTypes.GetMimeType (fileInfo.Name) + "; name=\"{0}\"",fileInfo.Name);
  189. aHeader.ContentDisposition =
  190. String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
  191. aHeader.ContentTransferEncoding = a.Encoding.ToString();
  192. smtp.WriteHeader( aHeader );
  193. // perform the actual writing of the file.
  194. // read from the file stream and write to the tcp stream
  195. FileStream ins = new FileStream( fileInfo.FullName , FileMode.Open );
  196. // create an apropriate encoder
  197. IAttachmentEncoder encoder;
  198. if( a.Encoding == MailEncoding.UUEncode ) {
  199. encoder = new UUAttachmentEncoder( 644 , fileInfo.Name );
  200. } else {
  201. encoder = new Base64AttachmentEncoder();
  202. }
  203. encoder.EncodeStream( ins , smtp.Stream );
  204. ins.Close();
  205. smtp.WriteLine( "" );
  206. // if it is the last attachment write
  207. // the final boundary otherwise write
  208. // a normal one.
  209. if( i < (msg.Attachments.Count - 1) ) {
  210. smtp.WriteBoundary( boundary );
  211. } else {
  212. smtp.WriteFinalBoundary( boundary );
  213. }
  214. }
  215. }
  216. // send quit command and
  217. // closes the connection
  218. public void Close() {
  219. smtp.WriteQuit();
  220. tcpConnection.Close();
  221. }
  222. }
  223. }