MailMessageWrapper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #if NET_2_0
  35. #pragma warning disable 618
  36. #endif
  37. // wraps a MailMessage to make an easier
  38. // interface to work with collections of
  39. // addresses instead of a single string
  40. internal class MailMessageWrapper {
  41. private MailAddressCollection bcc = new MailAddressCollection();
  42. private MailAddressCollection cc = new MailAddressCollection();
  43. private MailAddress from;
  44. private MailAddressCollection to = new MailAddressCollection();
  45. private MailHeader header = new MailHeader();
  46. private MailMessage message;
  47. private string body;
  48. // Constructor
  49. public MailMessageWrapper( MailMessage message )
  50. {
  51. this.message = message;
  52. if( message.From != null ) {
  53. from = MailAddress.Parse( message.From );
  54. header.From = from.ToString();
  55. }
  56. if( message.To != null ) {
  57. to = MailAddressCollection.Parse( message.To );
  58. header.To = to.ToString();
  59. }
  60. if( message.Cc != null ) {
  61. cc = MailAddressCollection.Parse( message.Cc );
  62. header.Cc = cc.ToString();
  63. }
  64. if( message.Bcc != null ) {
  65. bcc = MailAddressCollection.Parse( message.Bcc );
  66. header.Bcc = bcc.ToString();
  67. }
  68. // set the subject
  69. if( message.Subject != null ) {
  70. // encode the subject if it needs encoding
  71. if( MailUtil.NeedEncoding( message.Subject ) ) {
  72. byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
  73. // encode the subject with Base64
  74. header.Subject = String.Format( "=?{0}?B?{1}?=" ,
  75. message.BodyEncoding.BodyName ,
  76. Convert.ToBase64String( subjectBytes ) );
  77. } else {
  78. header.Subject = message.Subject;
  79. }
  80. }
  81. // convert single '.' on a line with ".." to not
  82. // confuse the smtp server since the DATA command
  83. // is terminated with a '.' on a single line.
  84. // this is also according to the smtp specs.
  85. if( message.Body != null ) {
  86. body = message.Body.Replace( "\n.\n" , "\n..\n" );
  87. body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
  88. }
  89. // set the Contet-Base header
  90. if( message.UrlContentBase != null )
  91. header.ContentBase = message.UrlContentBase;
  92. // set the Contet-Location header
  93. if( message.UrlContentLocation != null )
  94. header.ContentLocation = message.UrlContentLocation;
  95. // set the content type
  96. switch( message.BodyFormat ) {
  97. case MailFormat.Html:
  98. header.ContentType =
  99. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  100. break;
  101. case MailFormat.Text:
  102. header.ContentType =
  103. String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  104. break;
  105. default:
  106. header.ContentType =
  107. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  108. break;
  109. }
  110. // set the priority as in the same way as .NET sdk does
  111. switch( message.Priority ) {
  112. case MailPriority.High:
  113. header.Importance = "high";
  114. break;
  115. case MailPriority.Low:
  116. header.Importance = "low";
  117. break;
  118. case MailPriority.Normal:
  119. header.Importance = "normal";
  120. break;
  121. default:
  122. header.Importance = "normal";
  123. break;
  124. }
  125. // .NET sdk allways sets this to normal
  126. header.Priority = "normal";
  127. // Set the mime version
  128. header.MimeVersion = "1.0";
  129. // Set the transfer encoding
  130. if( message.BodyEncoding is ASCIIEncoding ) {
  131. header.ContentTransferEncoding = "7bit";
  132. } else {
  133. header.ContentTransferEncoding = "8bit";
  134. }
  135. // Add Date header, we were missing earlier 27/08/04
  136. // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
  137. //DateTime.Now gives in format 8/27/2004 8:13:00 PM
  138. // Need to explore further dateTime formats available or do we need
  139. // to write a function to convert.
  140. //header.Data.Add ("Date", DateTime.Now.ToString());
  141. // Add the custom headers
  142. foreach( string key in message.Headers.Keys )
  143. header.Data[ key ] = (string)this.message.Headers[ key ];
  144. }
  145. // Properties
  146. public IList Attachments {
  147. get { return message.Attachments; }
  148. }
  149. public MailAddressCollection Bcc {
  150. get { return bcc; }
  151. }
  152. public string Body {
  153. get { return body; }
  154. set { body = value; }
  155. }
  156. public Encoding BodyEncoding {
  157. get { return message.BodyEncoding; }
  158. set { message.BodyEncoding = value; }
  159. }
  160. public MailFormat BodyFormat {
  161. get { return message.BodyFormat; }
  162. set { message.BodyFormat = value; }
  163. }
  164. public MailAddressCollection Cc {
  165. get { return cc; }
  166. }
  167. public MailAddress From {
  168. get { return from; }
  169. }
  170. public MailHeader Header {
  171. get { return header; }
  172. }
  173. public MailPriority Priority {
  174. get { return message.Priority; }
  175. set { message.Priority = value; }
  176. }
  177. public string Subject {
  178. get { return message.Subject; }
  179. set { message.Subject = value; }
  180. }
  181. public MailAddressCollection To {
  182. get { return to; }
  183. }
  184. public string UrlContentBase {
  185. get { return message.UrlContentBase; }
  186. }
  187. public string UrlContentLocation {
  188. get { return message.UrlContentLocation; }
  189. }
  190. #if NET_1_1
  191. public MailHeader Fields {
  192. get {
  193. MailHeader bodyHeaders = new MailHeader();
  194. // Add Fields to MailHeader Object
  195. foreach( string key in message.Fields.Keys )
  196. bodyHeaders.Data[ key ] = (string)this.message.Fields[ key ];
  197. return bodyHeaders;
  198. }
  199. }
  200. #endif
  201. }
  202. #if NET_2_0
  203. #pragma warning restore 618
  204. #endif
  205. }