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. // if the BodyEncoding is not 7bit us-ascii then
  46. // convert the subject using base64
  47. if( message.BodyEncoding is ASCIIEncoding ) {
  48. header.Subject = message.Subject;
  49. } else {
  50. byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
  51. // encode the subject with Base64
  52. header.Subject = String.Format( "=?{0}?B?{1}?=" ,
  53. message.BodyEncoding.BodyName ,
  54. Convert.ToBase64String( subjectBytes ) );
  55. }
  56. }
  57. // convert single '.' on a line with ".." to not
  58. // confuse the smtp server since the DATA command
  59. // is terminated with a '.' on a single line.
  60. // this is also according to the smtp specs.
  61. if( message.Body != null ) {
  62. body = message.Body.Replace( "\n.\n" , "\n..\n" );
  63. body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
  64. }
  65. // set the Contet-Base header
  66. if( message.UrlContentBase != null )
  67. header.ContentBase = message.UrlContentBase;
  68. // set the Contet-Location header
  69. if( message.UrlContentLocation != null )
  70. header.ContentLocation = message.UrlContentLocation;
  71. // set the content type
  72. switch( message.BodyFormat ) {
  73. case MailFormat.Html:
  74. header.ContentType =
  75. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  76. break;
  77. case MailFormat.Text:
  78. header.ContentType =
  79. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  80. break;
  81. default:
  82. header.ContentType =
  83. String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
  84. break;
  85. }
  86. // set the priority as in the same way as .NET sdk does
  87. switch( message.Priority ) {
  88. case MailPriority.High:
  89. header.Importance = "high";
  90. break;
  91. case MailPriority.Low:
  92. header.Importance = "low";
  93. break;
  94. case MailPriority.Normal:
  95. header.Importance = "normal";
  96. break;
  97. default:
  98. header.Importance = "normal";
  99. break;
  100. }
  101. // .NET sdk allways sets this to normal
  102. header.Priority = "normal";
  103. // Set the mime version
  104. header.MimeVersion = "1.0";
  105. // Set the transfer encoding
  106. if( message.BodyEncoding is ASCIIEncoding ) {
  107. header.ContentTransferEncoding = "7bit";
  108. } else {
  109. header.ContentTransferEncoding = "8bit";
  110. }
  111. // Add the custom headers
  112. foreach( string key in message.Headers.Keys )
  113. header.Data[ key ] = (string)this.message.Headers[ key ];
  114. }
  115. // Properties
  116. public IList Attachments {
  117. get { return message.Attachments; }
  118. }
  119. public MailAddressCollection Bcc {
  120. get { return bcc; }
  121. }
  122. public string Body {
  123. get { return body; }
  124. set { body = value; }
  125. }
  126. public Encoding BodyEncoding {
  127. get { return message.BodyEncoding; }
  128. set { message.BodyEncoding = value; }
  129. }
  130. public MailFormat BodyFormat {
  131. get { return message.BodyFormat; }
  132. set { message.BodyFormat = value; }
  133. }
  134. public MailAddressCollection Cc {
  135. get { return cc; }
  136. }
  137. public MailAddress From {
  138. get { return from; }
  139. }
  140. public MailHeader Header {
  141. get { return header; }
  142. }
  143. public MailPriority Priority {
  144. get { return message.Priority; }
  145. set { message.Priority = value; }
  146. }
  147. public string Subject {
  148. get { return message.Subject; }
  149. set { message.Subject = value; }
  150. }
  151. public MailAddressCollection To {
  152. get { return to; }
  153. }
  154. public string UrlContentBase {
  155. get { return message.UrlContentBase; }
  156. }
  157. public string UrlContentLocation {
  158. get { return message.UrlContentLocation; }
  159. }
  160. }
  161. }