MailMessageWrapper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // System.Web.Mail.MailMessageWrapper.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. // Sanjay Gupta <[email protected]>
  7. //
  8. // (C) 2004, Novell, Inc. (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Text;
  33. namespace System.Web.Mail {
  34. // wraps a MailMessage to make an easier
  35. // interface to work with collections of
  36. // addresses instead of a single string
  37. internal class MailMessageWrapper {
  38. private MailAddressCollection bcc = new MailAddressCollection();
  39. private MailAddressCollection cc = new MailAddressCollection();
  40. private MailAddress from;
  41. private MailAddressCollection to = new MailAddressCollection();
  42. private MailHeader header = new MailHeader();
  43. private MailMessage message;
  44. private string body;
  45. // Constructor
  46. public MailMessageWrapper( MailMessage message )
  47. {
  48. this.message = message;
  49. if( message.From != null ) {
  50. from = MailAddress.Parse( message.From );
  51. header.From = from.ToString();
  52. }
  53. if( message.To != null ) {
  54. to = MailAddressCollection.Parse( message.To );
  55. header.To = to.ToString();
  56. }
  57. if( message.Cc != null ) {
  58. cc = MailAddressCollection.Parse( message.Cc );
  59. header.Cc = cc.ToString();
  60. }
  61. if( message.Bcc != null ) {
  62. bcc = MailAddressCollection.Parse( message.Bcc );
  63. header.Bcc = bcc.ToString();
  64. }
  65. // set the subject
  66. if( message.Subject != null ) {
  67. // encode the subject if it needs encoding
  68. if( MailUtil.NeedEncoding( message.Subject ) ) {
  69. byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
  70. // encode the subject with Base64
  71. header.Subject = String.Format( "=?{0}?B?{1}?=" ,
  72. message.BodyEncoding.BodyName ,
  73. Convert.ToBase64String( subjectBytes ) );
  74. } else {
  75. header.Subject = message.Subject;
  76. }
  77. }
  78. // convert single '.' on a line with ".." to not
  79. // confuse the smtp server since the DATA command
  80. // is terminated with a '.' on a single line.
  81. // this is also according to the smtp specs.
  82. if( message.Body != null ) {
  83. body = message.Body.Replace( "\n.\n" , "\n..\n" );
  84. body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
  85. }
  86. // set the Contet-Base header
  87. if( message.UrlContentBase != null )
  88. header.ContentBase = message.UrlContentBase;
  89. // set the Contet-Location header
  90. if( message.UrlContentLocation != null )
  91. header.ContentLocation = message.UrlContentLocation;
  92. // set the content type
  93. switch( message.BodyFormat ) {
  94. case MailFormat.Html:
  95. header.ContentType =
  96. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  97. break;
  98. case MailFormat.Text:
  99. header.ContentType =
  100. String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  101. break;
  102. default:
  103. header.ContentType =
  104. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  105. break;
  106. }
  107. // set the priority as in the same way as .NET sdk does
  108. switch( message.Priority ) {
  109. case MailPriority.High:
  110. header.Importance = "high";
  111. break;
  112. case MailPriority.Low:
  113. header.Importance = "low";
  114. break;
  115. case MailPriority.Normal:
  116. header.Importance = "normal";
  117. break;
  118. default:
  119. header.Importance = "normal";
  120. break;
  121. }
  122. // .NET sdk allways sets this to normal
  123. header.Priority = "normal";
  124. // Set the mime version
  125. header.MimeVersion = "1.0";
  126. // Set the transfer encoding
  127. if( message.BodyEncoding is ASCIIEncoding ) {
  128. header.ContentTransferEncoding = "7bit";
  129. } else {
  130. header.ContentTransferEncoding = "8bit";
  131. }
  132. // Add Date header, we were missing earlier 27/08/04
  133. // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
  134. //DateTime.Now gives in format 8/27/2004 8:13:00 PM
  135. // Need to explore further dateTime formats available or do we need
  136. // to write a function to convert.
  137. //header.Data.Add ("Date", DateTime.Now.ToString());
  138. // Add the custom headers
  139. foreach( string key in message.Headers.Keys )
  140. header.Data[ key ] = (string)this.message.Headers[ key ];
  141. }
  142. // Properties
  143. public IList Attachments {
  144. get { return message.Attachments; }
  145. }
  146. public MailAddressCollection Bcc {
  147. get { return bcc; }
  148. }
  149. public string Body {
  150. get { return body; }
  151. set { body = value; }
  152. }
  153. public Encoding BodyEncoding {
  154. get { return message.BodyEncoding; }
  155. set { message.BodyEncoding = value; }
  156. }
  157. public MailFormat BodyFormat {
  158. get { return message.BodyFormat; }
  159. set { message.BodyFormat = value; }
  160. }
  161. public MailAddressCollection Cc {
  162. get { return cc; }
  163. }
  164. public MailAddress From {
  165. get { return from; }
  166. }
  167. public MailHeader Header {
  168. get { return header; }
  169. }
  170. public MailPriority Priority {
  171. get { return message.Priority; }
  172. set { message.Priority = value; }
  173. }
  174. public string Subject {
  175. get { return message.Subject; }
  176. set { message.Subject = value; }
  177. }
  178. public MailAddressCollection To {
  179. get { return to; }
  180. }
  181. public string UrlContentBase {
  182. get { return message.UrlContentBase; }
  183. }
  184. public string UrlContentLocation {
  185. get { return message.UrlContentLocation; }
  186. }
  187. #if NET_1_1
  188. public MailHeader Fields {
  189. get {
  190. MailHeader bodyHeaders = new MailHeader();
  191. // Add Fields to MailHeader Object
  192. foreach( string key in message.Fields.Keys )
  193. bodyHeaders.Data[ key ] = this.message.Fields[ key ].ToString();
  194. return bodyHeaders;
  195. }
  196. }
  197. #endif
  198. }
  199. }