2 // System.Web.Mail.MailMessageWrapper.cs
5 // Per Arneng <pt99par@student.bth.se>
6 // Sanjay Gupta <gsanjay@novell.com>
8 // (C) 2004, Novell, Inc. (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
34 namespace System.Web.Mail {
36 // wraps a MailMessage to make an easier
37 // interface to work with collections of
38 // addresses instead of a single string
39 internal class MailMessageWrapper {
41 MailAddressCollection bcc = new MailAddressCollection();
42 MailAddressCollection cc = new MailAddressCollection();
44 MailAddressCollection to = new MailAddressCollection();
45 MailHeader header = new MailHeader();
50 public MailMessageWrapper( MailMessage message )
52 this.message = message;
54 if( message.From != null ) {
55 from = MailAddress.Parse( message.From );
56 header.From = from.ToString();
59 if( message.To != null ) {
60 to = MailAddressCollection.Parse( message.To );
61 header.To = to.ToString();
64 if( message.Cc != null ) {
65 cc = MailAddressCollection.Parse( message.Cc );
66 header.Cc = cc.ToString();
69 if( message.Bcc != null ) {
70 bcc = MailAddressCollection.Parse( message.Bcc );
71 header.Bcc = bcc.ToString();
75 if( message.Subject != null ) {
77 // encode the subject if it needs encoding
78 if( MailUtil.NeedEncoding( message.Subject ) ) {
80 byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
81 // encode the subject with Base64
82 header.Subject = "=?" + message.BodyEncoding.BodyName + "?B?" + Convert.ToBase64String (subjectBytes) + "?=";
85 header.Subject = message.Subject;
90 // convert single '.' on a line with ".." to not
91 // confuse the smtp server since the DATA command
92 // is terminated with a '.' on a single line.
93 // this is also according to the smtp specs.
94 if( message.Body != null ) {
95 body = message.Body.Replace( "\n.\n" , "\n..\n" );
96 body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
100 // set the Contet-Base header
101 if( message.UrlContentBase != null )
102 header.ContentBase = message.UrlContentBase;
104 // set the Contet-Location header
105 if( message.UrlContentLocation != null )
106 header.ContentLocation = message.UrlContentLocation;
109 // set the content type
110 switch( message.BodyFormat ) {
111 case MailFormat.Html:
112 header.ContentType = String.Concat ( "text/html; charset=\"", message.BodyEncoding.BodyName, "\"");
115 case MailFormat.Text:
116 header.ContentType = String.Concat ( "text/plain; charset=\"", message.BodyEncoding.BodyName, "\"");
120 header.ContentType = String.Concat ( "text/html; charset=\"", message.BodyEncoding.BodyName, "\"");
125 // set the priority as in the same way as .NET sdk does
126 switch( message.Priority ) {
128 case MailPriority.High:
129 header.Importance = "high";
132 case MailPriority.Low:
133 header.Importance = "low";
136 case MailPriority.Normal:
137 header.Importance = "normal";
141 header.Importance = "normal";
146 // .NET sdk allways sets this to normal
147 header.Priority = "normal";
150 // Set the mime version
151 header.MimeVersion = "1.0";
153 // Set the transfer encoding
154 if( message.BodyEncoding is ASCIIEncoding ) {
155 header.ContentTransferEncoding = "7bit";
157 header.ContentTransferEncoding = "8bit";
160 // Add Date header, we were missing earlier 27/08/04
161 // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
162 //DateTime.Now gives in format 8/27/2004 8:13:00 PM
163 // Need to explore further dateTime formats available or do we need
164 // to write a function to convert.
165 //header.Data.Add ("Date", DateTime.Now.ToString());
167 // Add the custom headers
168 foreach( string key in message.Headers.Keys )
169 header.Data[ key ] = (string)this.message.Headers[ key ];
173 public IList Attachments {
174 get { return message.Attachments; }
177 public MailAddressCollection Bcc {
183 set { body = value; }
186 public Encoding BodyEncoding {
187 get { return message.BodyEncoding; }
188 set { message.BodyEncoding = value; }
191 public MailFormat BodyFormat {
192 get { return message.BodyFormat; }
193 set { message.BodyFormat = value; }
196 public MailAddressCollection Cc {
200 public MailAddress From {
204 public MailHeader Header {
205 get { return header; }
208 public MailPriority Priority {
209 get { return message.Priority; }
210 set { message.Priority = value; }
213 public string Subject {
214 get { return message.Subject; }
215 set { message.Subject = value; }
218 public MailAddressCollection To {
222 public string UrlContentBase {
223 get { return message.UrlContentBase; }
227 public string UrlContentLocation {
228 get { return message.UrlContentLocation; }
232 public MailHeader Fields {
234 MailHeader bodyHeaders = new MailHeader();
235 // Add Fields to MailHeader Object
236 foreach( string key in message.Fields.Keys )
237 bodyHeaders.Data[ key ] = this.message.Fields[ key ].ToString();