SmtpClient.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SmtpClient.cs
  2. // author: Per Arneng <[email protected]>
  3. using System;
  4. using System.Net;
  5. using System.IO;
  6. using System.Text;
  7. using System.Collections;
  8. using System.Net.Sockets;
  9. namespace System.Web.Mail {
  10. /// represents a conntection to a smtp server
  11. internal class SmtpClient {
  12. private string server;
  13. private TcpClient tcpConnection;
  14. private SmtpStream smtp;
  15. private Encoding encoding;
  16. //Initialise the variables and connect
  17. public SmtpClient( string server ) {
  18. this.server = server;
  19. encoding = new ASCIIEncoding( );
  20. Connect();
  21. }
  22. // make the actual connection
  23. // and HELO handshaking
  24. private void Connect() {
  25. tcpConnection = new TcpClient( server , 25 );
  26. Stream stream = tcpConnection.GetStream();
  27. smtp = new SmtpStream( stream );
  28. // read the server greeting
  29. smtp.ReadResponse();
  30. smtp.CheckForStatusCode( 220 );
  31. // write the HELO command to the server
  32. smtp.WriteHelo( Dns.GetHostName() );
  33. }
  34. public void Send( MailMessageWrapper msg ) {
  35. if( msg.From == null ) {
  36. throw new SmtpException( "From property must be set." );
  37. }
  38. if( msg.To == null ) {
  39. if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
  40. }
  41. // if no encoding is set then set the system
  42. // default encoding
  43. if( msg.BodyEncoding == null )
  44. msg.BodyEncoding = Encoding.Default;
  45. // start with a reset incase old data
  46. // is present at the server in this session
  47. smtp.WriteRset();
  48. // write the mail from command
  49. smtp.WriteMailFrom( msg.From.Address );
  50. // write the rcpt to command for the To addresses
  51. foreach( MailAddress addr in msg.To ) {
  52. smtp.WriteRcptTo( addr.Address );
  53. }
  54. // write the rcpt to command for the Cc addresses
  55. foreach( MailAddress addr in msg.Cc ) {
  56. smtp.WriteRcptTo( addr.Address );
  57. }
  58. // write the data command and then
  59. // send the email
  60. smtp.WriteData();
  61. if( msg.Attachments.Count == 0 ) {
  62. SendSinglepartMail( msg );
  63. } else {
  64. SendMultipartMail( msg );
  65. }
  66. // write the data end tag "."
  67. smtp.WriteDataEndTag();
  68. }
  69. // sends a single part mail to the server
  70. private void SendSinglepartMail( MailMessageWrapper msg ) {
  71. // create the headers
  72. IDictionary headers = CreateHeaders( msg );
  73. smtp.WriteHeaders( headers );
  74. // send the mail body FIXME
  75. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  76. }
  77. // sends a multipart mail to the server
  78. private void SendMultipartMail( MailMessageWrapper msg ) {
  79. // create the headers
  80. IDictionary headers = CreateHeaders( msg );
  81. // set the part boundary FIXME: THIS SHOULD NOT BE HARDCODED
  82. // look att Gaurav Vaish implementation
  83. string boundary = "NextPart_000_1113_1962_1fe8";
  84. // set the Content-Type header to multipart/mixed
  85. headers[ "Content-Type" ] =
  86. String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
  87. // write the headers
  88. // and start writing the multipart body
  89. smtp.WriteHeaders( headers );
  90. // write the first part text part
  91. // before the attachments
  92. smtp.WriteBoundary( boundary );
  93. Hashtable partHeaders = new Hashtable();
  94. partHeaders[ "Content-Type" ] = "text/plain";
  95. smtp.WriteHeaders( partHeaders );
  96. // FIXME: probably need to use QP or Base64 on everything higher
  97. // then 8-bit .. like utf-16
  98. smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
  99. smtp.WriteBoundary( boundary );
  100. // now start to write the attachments
  101. for( int i=0; i< msg.Attachments.Count ; i++ ) {
  102. MailAttachment a = (MailAttachment)msg.Attachments[ i ];
  103. Hashtable aHeaders = new Hashtable();
  104. aHeaders[ "Content-Type" ] =
  105. String.Format( "application/octet-stream; name=\"{0}\"",
  106. a.Filename );
  107. aHeaders[ "Content-Disposition" ] =
  108. String.Format( "attachment; filename=\"{0}\"" , a.Filename );
  109. aHeaders[ "Content-Transfer-Encoding" ] = "base64";
  110. smtp.WriteHeaders( aHeaders );
  111. // perform the actual writing of the file.
  112. // read from the file stream and write to the tcp stream
  113. FileStream ins = new FileStream( a.Filename , FileMode.Open );
  114. // create an apropriate encoder
  115. MailEncoder encoder = new MailEncoder( a.Encoding );
  116. encoder.EncodeStream( ins , smtp.Stream );
  117. ins.Close();
  118. smtp.WriteLine( "" );
  119. // if it is the last attachment write
  120. // the final boundary otherwise write
  121. // a normal one.
  122. if( i < (msg.Attachments.Count - 1) ) {
  123. smtp.WriteBoundary( boundary );
  124. } else {
  125. smtp.WriteFinalBoundary( boundary );
  126. }
  127. }
  128. }
  129. // send the standard headers
  130. // and the custom in MailMessage
  131. private IDictionary CreateHeaders( MailMessageWrapper msg ) {
  132. Hashtable headers = new Hashtable();
  133. headers[ "From" ] = msg.From.ToString();
  134. headers[ "To" ] = msg.To.ToString();
  135. if( msg.Cc.Count > 0 ) headers[ "Cc" ] = msg.Cc.ToString();
  136. if( msg.Bcc.Count > 0 ) headers[ "Bcc" ] = msg.Bcc.ToString();
  137. if( HasData( msg.Subject ) ) {
  138. // if the BodyEncoding is not 7bit us-ascii then
  139. // convert using base64
  140. if( msg.BodyEncoding is ASCIIEncoding ) {
  141. headers[ "Subject" ] = msg.Subject;
  142. } else {
  143. byte[] subjectBytes = msg.BodyEncoding.GetBytes( msg.Subject );
  144. // encode the subject with Base64
  145. headers[ "Subject" ] =
  146. String.Format( "=?{0}?{1}?{2}?=" ,
  147. msg.BodyEncoding.BodyName , "B",
  148. Convert.ToBase64String( subjectBytes ) );
  149. }
  150. }
  151. if( HasData( msg.UrlContentBase ) )
  152. headers[ "Content-Base" ] = msg.UrlContentBase;
  153. if( HasData( msg.UrlContentLocation ) )
  154. headers[ "Content-Location" ] = msg.UrlContentLocation;
  155. string charset = String.Format( "charset=\"{0}\"" , msg.BodyEncoding.BodyName );
  156. // set body the content type
  157. switch( msg.BodyFormat ) {
  158. case MailFormat.Html:
  159. headers[ "Content-Type" ] = "text/html; " + charset;
  160. break;
  161. case MailFormat.Text:
  162. headers[ "Content-Type" ] = "text/plain; " + charset;
  163. break;
  164. default:
  165. headers[ "Content-Type" ] = "text/plain; " + charset;
  166. break;
  167. }
  168. // set the priority as in the same way as .NET sdk does
  169. switch( msg.Priority ) {
  170. case MailPriority.High:
  171. headers[ "Importance" ] = "high";
  172. break;
  173. case MailPriority.Low:
  174. headers[ "Importance" ] = "low";
  175. break;
  176. case MailPriority.Normal:
  177. headers[ "Importance" ] = "normal";
  178. break;
  179. default:
  180. headers[ "Importance" ] = "normal";
  181. break;
  182. }
  183. // .NET sdk allways sets this to normal
  184. headers[ "Priority" ] = "normal";
  185. // add mime version
  186. headers[ "Mime-Version" ] = "1.0";
  187. // set the mailer -- should probably be changed
  188. headers[ "X-Mailer" ] = "Mono (System.Web.Mail.SmtpMail.Send)";
  189. // Set the transfer encoding.. it seems like only sends 7bit
  190. // if it is ASCII
  191. if( msg.BodyEncoding is ASCIIEncoding ) {
  192. headers[ "Content-Transfer-Encoding" ] = "7bit";
  193. } else {
  194. headers[ "Content-Transfer-Encoding" ] = "8bit";
  195. }
  196. // add the custom headers they will overwrite
  197. // the earlier ones if they are the same
  198. foreach( string key in msg.Headers.Keys )
  199. headers[ key ] = (string)msg.Headers[ key ];
  200. return headers;
  201. }
  202. // returns true if str is not null and not
  203. // empty
  204. private bool HasData( string str ) {
  205. bool hasData = false;
  206. if( str != null ) {
  207. if( str.Length > 0 ) {
  208. hasData = true;
  209. }
  210. }
  211. return hasData;
  212. }
  213. // send quit command and
  214. // closes the connection
  215. public void Close() {
  216. smtp.WriteQuit();
  217. tcpConnection.Close();
  218. }
  219. }
  220. }