MailMessageWrapper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // System.Web.Mail.MailMessageWrapper.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Text;
  11. namespace System.Web.Mail {
  12. // wraps a MailMessage to make an easier
  13. // interface to work with collections of
  14. // addresses instead of a single string
  15. internal class MailMessageWrapper {
  16. private MailAddressCollection bcc = new MailAddressCollection();
  17. private MailAddressCollection cc = new MailAddressCollection();
  18. private MailAddress from;
  19. private MailAddressCollection to = new MailAddressCollection();
  20. private MailHeader header = new MailHeader();
  21. private MailMessage message;
  22. private string body;
  23. // Constructor
  24. public MailMessageWrapper( MailMessage message )
  25. {
  26. this.message = message;
  27. if( message.From != null ) {
  28. from = MailAddress.Parse( message.From );
  29. header.From = from.ToString();
  30. }
  31. if( message.To != null ) {
  32. to = MailAddressCollection.Parse( message.To );
  33. header.To = to.ToString();
  34. }
  35. if( message.Cc != null ) {
  36. cc = MailAddressCollection.Parse( message.Cc );
  37. header.Cc = cc.ToString();
  38. }
  39. if( message.Bcc != null ) {
  40. bcc = MailAddressCollection.Parse( message.Bcc );
  41. header.Bcc = bcc.ToString();
  42. }
  43. // set the subject
  44. if( message.Subject != null ) {
  45. // encode the subject if it needs encoding
  46. if( MailUtil.NeedEncoding( message.Subject ) ) {
  47. byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
  48. // encode the subject with Base64
  49. header.Subject = String.Format( "=?{0}?B?{1}?=" ,
  50. message.BodyEncoding.BodyName ,
  51. Convert.ToBase64String( subjectBytes ) );
  52. } else {
  53. header.Subject = message.Subject;
  54. }
  55. }
  56. // convert single '.' on a line with ".." to not
  57. // confuse the smtp server since the DATA command
  58. // is terminated with a '.' on a single line.
  59. // this is also according to the smtp specs.
  60. if( message.Body != null ) {
  61. body = message.Body.Replace( "\n.\n" , "\n..\n" );
  62. body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
  63. }
  64. // set the Contet-Base header
  65. if( message.UrlContentBase != null )
  66. header.ContentBase = message.UrlContentBase;
  67. // set the Contet-Location header
  68. if( message.UrlContentLocation != null )
  69. header.ContentLocation = message.UrlContentLocation;
  70. // set the content type
  71. switch( message.BodyFormat ) {
  72. case MailFormat.Html:
  73. header.ContentType =
  74. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  75. break;
  76. case MailFormat.Text:
  77. header.ContentType =
  78. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  79. break;
  80. default:
  81. header.ContentType =
  82. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  83. break;
  84. }
  85. // set the priority as in the same way as .NET sdk does
  86. switch( message.Priority ) {
  87. case MailPriority.High:
  88. header.Importance = "high";
  89. break;
  90. case MailPriority.Low:
  91. header.Importance = "low";
  92. break;
  93. case MailPriority.Normal:
  94. header.Importance = "normal";
  95. break;
  96. default:
  97. header.Importance = "normal";
  98. break;
  99. }
  100. // .NET sdk allways sets this to normal
  101. header.Priority = "normal";
  102. // Set the mime version
  103. header.MimeVersion = "1.0";
  104. // Set the transfer encoding
  105. if( message.BodyEncoding is ASCIIEncoding ) {
  106. header.ContentTransferEncoding = "7bit";
  107. } else {
  108. header.ContentTransferEncoding = "8bit";
  109. }
  110. // Add the custom headers
  111. foreach( string key in message.Headers.Keys )
  112. header.Data[ key ] = (string)this.message.Headers[ key ];
  113. }
  114. // Properties
  115. public IList Attachments {
  116. get { return message.Attachments; }
  117. }
  118. public MailAddressCollection Bcc {
  119. get { return bcc; }
  120. }
  121. public string Body {
  122. get { return body; }
  123. set { body = value; }
  124. }
  125. public Encoding BodyEncoding {
  126. get { return message.BodyEncoding; }
  127. set { message.BodyEncoding = value; }
  128. }
  129. public MailFormat BodyFormat {
  130. get { return message.BodyFormat; }
  131. set { message.BodyFormat = value; }
  132. }
  133. public MailAddressCollection Cc {
  134. get { return cc; }
  135. }
  136. public MailAddress From {
  137. get { return from; }
  138. }
  139. public MailHeader Header {
  140. get { return header; }
  141. }
  142. public MailPriority Priority {
  143. get { return message.Priority; }
  144. set { message.Priority = value; }
  145. }
  146. public string Subject {
  147. get { return message.Subject; }
  148. set { message.Subject = value; }
  149. }
  150. public MailAddressCollection To {
  151. get { return to; }
  152. }
  153. public string UrlContentBase {
  154. get { return message.UrlContentBase; }
  155. }
  156. public string UrlContentLocation {
  157. get { return message.UrlContentLocation; }
  158. }
  159. }
  160. }