MailMessageWrapper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.Web.Mail.MailMessageWrapper.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Text;
  31. namespace System.Web.Mail {
  32. // wraps a MailMessage to make an easier
  33. // interface to work with collections of
  34. // addresses instead of a single string
  35. internal class MailMessageWrapper {
  36. private MailAddressCollection bcc = new MailAddressCollection();
  37. private MailAddressCollection cc = new MailAddressCollection();
  38. private MailAddress from;
  39. private MailAddressCollection to = new MailAddressCollection();
  40. private MailHeader header = new MailHeader();
  41. private MailMessage message;
  42. private string body;
  43. // Constructor
  44. public MailMessageWrapper( MailMessage message )
  45. {
  46. this.message = message;
  47. if( message.From != null ) {
  48. from = MailAddress.Parse( message.From );
  49. header.From = from.ToString();
  50. }
  51. if( message.To != null ) {
  52. to = MailAddressCollection.Parse( message.To );
  53. header.To = to.ToString();
  54. }
  55. if( message.Cc != null ) {
  56. cc = MailAddressCollection.Parse( message.Cc );
  57. header.Cc = cc.ToString();
  58. }
  59. if( message.Bcc != null ) {
  60. bcc = MailAddressCollection.Parse( message.Bcc );
  61. header.Bcc = bcc.ToString();
  62. }
  63. // set the subject
  64. if( message.Subject != null ) {
  65. // encode the subject if it needs encoding
  66. if( MailUtil.NeedEncoding( message.Subject ) ) {
  67. byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
  68. // encode the subject with Base64
  69. header.Subject = String.Format( "=?{0}?B?{1}?=" ,
  70. message.BodyEncoding.BodyName ,
  71. 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 =
  94. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  95. break;
  96. case MailFormat.Text:
  97. header.ContentType =
  98. String.Format( "text/plain; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  99. break;
  100. default:
  101. header.ContentType =
  102. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  103. break;
  104. }
  105. // set the priority as in the same way as .NET sdk does
  106. switch( message.Priority ) {
  107. case MailPriority.High:
  108. header.Importance = "high";
  109. break;
  110. case MailPriority.Low:
  111. header.Importance = "low";
  112. break;
  113. case MailPriority.Normal:
  114. header.Importance = "normal";
  115. break;
  116. default:
  117. header.Importance = "normal";
  118. break;
  119. }
  120. // .NET sdk allways sets this to normal
  121. header.Priority = "normal";
  122. // Set the mime version
  123. header.MimeVersion = "1.0";
  124. // Set the transfer encoding
  125. if( message.BodyEncoding is ASCIIEncoding ) {
  126. header.ContentTransferEncoding = "7bit";
  127. } else {
  128. header.ContentTransferEncoding = "8bit";
  129. }
  130. // Add the custom headers
  131. foreach( string key in message.Headers.Keys )
  132. header.Data[ key ] = (string)this.message.Headers[ key ];
  133. }
  134. // Properties
  135. public IList Attachments {
  136. get { return message.Attachments; }
  137. }
  138. public MailAddressCollection Bcc {
  139. get { return bcc; }
  140. }
  141. public string Body {
  142. get { return body; }
  143. set { body = value; }
  144. }
  145. public Encoding BodyEncoding {
  146. get { return message.BodyEncoding; }
  147. set { message.BodyEncoding = value; }
  148. }
  149. public MailFormat BodyFormat {
  150. get { return message.BodyFormat; }
  151. set { message.BodyFormat = value; }
  152. }
  153. public MailAddressCollection Cc {
  154. get { return cc; }
  155. }
  156. public MailAddress From {
  157. get { return from; }
  158. }
  159. public MailHeader Header {
  160. get { return header; }
  161. }
  162. public MailPriority Priority {
  163. get { return message.Priority; }
  164. set { message.Priority = value; }
  165. }
  166. public string Subject {
  167. get { return message.Subject; }
  168. set { message.Subject = value; }
  169. }
  170. public MailAddressCollection To {
  171. get { return to; }
  172. }
  173. public string UrlContentBase {
  174. get { return message.UrlContentBase; }
  175. }
  176. public string UrlContentLocation {
  177. get { return message.UrlContentLocation; }
  178. }
  179. }
  180. }