2
0

MailMessageWrapper.cs 6.6 KB

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