2 // System.Web.Mail.MailMessageWrapper.cs
5 // Per Arneng <pt99par@student.bth.se>
9 using System.Collections;
12 namespace System.Web.Mail {
14 // wraps a MailMessage to make an easier
15 // interface to work with collections of
16 // addresses instead of a single string
17 internal class MailMessageWrapper {
19 private MailAddressCollection bcc = new MailAddressCollection();
20 private MailAddressCollection cc = new MailAddressCollection();
21 private MailAddress from;
22 private MailAddressCollection to = new MailAddressCollection();
23 private MailHeader header = new MailHeader();
24 private MailMessage message;
28 public MailMessageWrapper( MailMessage message )
30 this.message = message;
32 if( message.From != null ) {
33 from = MailAddress.Parse( message.From );
34 header.From = from.ToString();
37 if( message.To != null ) {
38 to = MailAddressCollection.Parse( message.To );
39 header.To = to.ToString();
42 if( message.Cc != null ) {
43 cc = MailAddressCollection.Parse( message.Cc );
44 header.Cc = cc.ToString();
47 if( message.Bcc != null ) {
48 bcc = MailAddressCollection.Parse( message.Bcc );
49 header.Bcc = bcc.ToString();
54 if( message.Subject != null ) {
56 // encode the subject if it needs encoding
57 if( MailUtil.NeedEncoding( message.Subject ) ) {
59 byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
60 // encode the subject with Base64
61 header.Subject = String.Format( "=?{0}?B?{1}?=" ,
62 message.BodyEncoding.BodyName ,
63 Convert.ToBase64String( subjectBytes ) );
66 header.Subject = message.Subject;
71 // convert single '.' on a line with ".." to not
72 // confuse the smtp server since the DATA command
73 // is terminated with a '.' on a single line.
74 // this is also according to the smtp specs.
75 if( message.Body != null ) {
76 body = message.Body.Replace( "\n.\n" , "\n..\n" );
77 body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
81 // set the Contet-Base header
82 if( message.UrlContentBase != null )
83 header.ContentBase = message.UrlContentBase;
85 // set the Contet-Location header
86 if( message.UrlContentLocation != null )
87 header.ContentLocation = message.UrlContentLocation;
90 // set the content type
91 switch( message.BodyFormat ) {
95 String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
100 String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
105 String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
110 // set the priority as in the same way as .NET sdk does
111 switch( message.Priority ) {
113 case MailPriority.High:
114 header.Importance = "high";
117 case MailPriority.Low:
118 header.Importance = "low";
121 case MailPriority.Normal:
122 header.Importance = "normal";
126 header.Importance = "normal";
131 // .NET sdk allways sets this to normal
132 header.Priority = "normal";
135 // Set the mime version
136 header.MimeVersion = "1.0";
138 // Set the transfer encoding
139 if( message.BodyEncoding is ASCIIEncoding ) {
140 header.ContentTransferEncoding = "7bit";
142 header.ContentTransferEncoding = "8bit";
146 // Add the custom headers
147 foreach( string key in message.Headers.Keys )
148 header.Data[ key ] = (string)this.message.Headers[ key ];
152 public IList Attachments {
153 get { return message.Attachments; }
156 public MailAddressCollection Bcc {
162 set { body = value; }
165 public Encoding BodyEncoding {
166 get { return message.BodyEncoding; }
167 set { message.BodyEncoding = value; }
170 public MailFormat BodyFormat {
171 get { return message.BodyFormat; }
172 set { message.BodyFormat = value; }
175 public MailAddressCollection Cc {
179 public MailAddress From {
183 public MailHeader Header {
184 get { return header; }
187 public MailPriority Priority {
188 get { return message.Priority; }
189 set { message.Priority = value; }
192 public string Subject {
193 get { return message.Subject; }
194 set { message.Subject = value; }
197 public MailAddressCollection To {
201 public string UrlContentBase {
202 get { return message.UrlContentBase; }
206 public string UrlContentLocation {
207 get { return message.UrlContentLocation; }