--- /dev/null
+//
+// System.Web.Mail.MailHeader.cs
+//
+// Author(s):
+// Per Arneng <pt99par@student.bth.se>
+//
+//
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+
+namespace System.Web.Mail {
+
+ // This class represents the header of a mail with
+ // all the header fields.
+ internal class MailHeader {
+
+ protected NameValueCollection data = new NameValueCollection();
+
+ public string To {
+ get { return data[ "To" ]; }
+ set { data[ "To" ] = value; }
+ }
+
+ public string From {
+ get { return data[ "From" ]; }
+ set { data[ "From" ] = value; }
+ }
+
+ public string Cc {
+ get { return data[ "Cc" ]; }
+ set { data[ "Cc" ] = value; }
+ }
+
+ public string Bcc {
+ get { return data[ "Bcc" ]; }
+ set { data[ "Bcc" ] = value; }
+ }
+
+ public string Subject {
+ get { return data[ "Subject" ]; }
+ set { data[ "Subject" ] = value; }
+ }
+
+ public string Importance {
+ get { return data[ "Importance" ]; }
+ set { data[ "Importance" ] = value; }
+ }
+
+ public string Priority {
+ get { return data[ "Priority" ]; }
+ set { data[ "Priority" ] = value; }
+ }
+
+ public string MimeVersion {
+ get { return data[ "Mime-Version" ]; }
+ set { data[ "Mime-Version" ] = value; }
+ }
+
+ public string ContentType {
+ get { return data[ "Content-Type" ]; }
+ set { data[ "Content-Type" ] = value; }
+ }
+
+ public string ContentTransferEncoding{
+ get { return data[ "Content-Transfer-Encoding" ]; }
+ set { data[ "Content-Transfer-Encoding" ] = value; }
+ }
+
+ public string ContentDisposition {
+ get { return data[ "Content-Disposition" ]; }
+ set { data[ "Content-Disposition" ] = value; }
+ }
+
+ public string ContentBase {
+ get { return data[ "Content-Base" ]; }
+ set { data[ "Content-Base" ] = value; }
+ }
+
+ public string ContentLocation {
+ get { return data[ "Content-Location" ]; }
+ set { data[ "Content-Location" ] = value; }
+ }
+
+
+ public NameValueCollection Data {
+ get { return data; }
+ }
+
+ }
+
+}
private MailAddressCollection cc = new MailAddressCollection();
private MailAddress from;
private MailAddressCollection to = new MailAddressCollection();
+ private MailHeader header = new MailHeader();
private MailMessage message;
// Constructor
{
this.message = message;
- if(message.From != null ) from = MailAddress.Parse( message.From );
- if(message.To != null ) to = MailAddressCollection.Parse( message.To );
- if(message.Cc != null )cc = MailAddressCollection.Parse( message.Cc );
- if(message.Bcc != null )bcc = MailAddressCollection.Parse( message.Bcc );
+ if( message.From != null ) {
+ from = MailAddress.Parse( message.From );
+ header.From = from.ToString();
+ }
+
+ if( message.To != null ) {
+ to = MailAddressCollection.Parse( message.To );
+ header.To = to.ToString();
+ }
+
+ if( message.Cc != null ) {
+ cc = MailAddressCollection.Parse( message.Cc );
+ header.Cc = cc.ToString();
+ }
+
+ if( message.Bcc != null ) {
+ bcc = MailAddressCollection.Parse( message.Bcc );
+ header.Bcc = bcc.ToString();
+ }
+
+
+ // set the subject
+ if( message.Subject != null ) {
+ // if the BodyEncoding is not 7bit us-ascii then
+ // convert the subject using base64
+ if( message.BodyEncoding is ASCIIEncoding ) {
+
+ header.Subject = message.Subject;
+
+ } else {
+
+ byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
+ // encode the subject with Base64
+ header.Subject = String.Format( "=?{0}?B?{1}?=" ,
+ message.BodyEncoding.BodyName ,
+ Convert.ToBase64String( subjectBytes ) );
+ }
+ }
+
+
+ // set the Contet-Base header
+ if( message.UrlContentBase != null )
+ header.ContentBase = message.UrlContentBase;
+
+ // set the Contet-Location header
+ if( message.UrlContentLocation != null )
+ header.ContentLocation = message.UrlContentLocation;
+
+
+ // set the content type
+ switch( message.BodyFormat ) {
+
+ case MailFormat.Html:
+ header.ContentType =
+ String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+ break;
+
+ case MailFormat.Text:
+ header.ContentType =
+ String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+ break;
+
+ default:
+ header.ContentType =
+ String.Format( "text/html; charset=\"{0}\"" , message.BodyEncoding.BodyName );
+ break;
+ }
+
+
+ // set the priority as in the same way as .NET sdk does
+ switch( message.Priority ) {
+
+ case MailPriority.High:
+ header.Importance = "high";
+ break;
+
+ case MailPriority.Low:
+ header.Importance = "low";
+ break;
+
+ case MailPriority.Normal:
+ header.Importance = "normal";
+ break;
+
+ default:
+ header.Importance = "normal";
+ break;
+
+ }
+
+ // .NET sdk allways sets this to normal
+ header.Priority = "normal";
+
+
+ // Set the mime version
+ header.MimeVersion = "1.0";
+
+ // Set the transfer encoding
+ if( message.BodyEncoding is ASCIIEncoding ) {
+ header.ContentTransferEncoding = "7bit";
+ } else {
+ header.ContentTransferEncoding = "8bit";
+ }
+
+
+ // Add the custom headers
+ foreach( string key in message.Headers.Keys )
+ header.Data[ key ] = (string)this.message.Headers[ key ];
}
// Properties
get { return from; }
}
- public IDictionary Headers {
- get { return message.Headers; }
+ public MailHeader Header {
+ get { return header; }
}
public MailPriority Priority {
if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
}
- // if no encoding is set then set the system
- // default encoding
- if( msg.BodyEncoding == null )
- msg.BodyEncoding = Encoding.Default;
-
+
// start with a reset incase old data
// is present at the server in this session
smtp.WriteRset();
// sends a single part mail to the server
private void SendSinglepartMail( MailMessageWrapper msg ) {
- // create the headers
- IDictionary headers = CreateHeaders( msg );
-
- smtp.WriteHeaders( headers );
+ // write the header
+ smtp.WriteHeader( msg.Header );
// send the mail body FIXME
smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );
// sends a multipart mail to the server
private void SendMultipartMail( MailMessageWrapper msg ) {
-
- // create the headers
- IDictionary headers = CreateHeaders( msg );
-
+
// set the part boundary FIXME: THIS SHOULD NOT BE HARDCODED
// look att Gaurav Vaish implementation
string boundary = "NextPart_000_1113_1962_1fe8";
// set the Content-Type header to multipart/mixed
- headers[ "Content-Type" ] =
+ msg.Header.ContentType =
String.Format( "multipart/mixed;\r\n boundary={0}" , boundary );
- // write the headers
- // and start writing the multipart body
- smtp.WriteHeaders( headers );
+ // write the header
+ smtp.WriteHeader( msg.Header );
// write the first part text part
// before the attachments
smtp.WriteBoundary( boundary );
- Hashtable partHeaders = new Hashtable();
- partHeaders[ "Content-Type" ] = "text/plain";
+ MailHeader partHeader = new MailHeader();
+ partHeader.ContentType = "text/plain";
- smtp.WriteHeaders( partHeaders );
+ smtp.WriteHeader( partHeader );
// FIXME: probably need to use QP or Base64 on everything higher
FileInfo fileInfo = new FileInfo( a.Filename );
- Hashtable aHeaders = new Hashtable();
+ MailHeader aHeader = new MailHeader();
- aHeaders[ "Content-Type" ] =
+ aHeader.ContentType =
String.Format( "application/octet-stream; name=\"{0}\"",
fileInfo.Name );
- aHeaders[ "Content-Disposition" ] =
+ aHeader.ContentDisposition =
String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
- aHeaders[ "Content-Transfer-Encoding" ] =
- (a.Encoding == MailEncoding.UUEncode ? "UUEncode" : "Base64" );
-
- smtp.WriteHeaders( aHeaders );
+ aHeader.ContentTransferEncoding = a.Encoding.ToString();
+
+ smtp.WriteHeader( aHeader );
// perform the actual writing of the file.
// read from the file stream and write to the tcp stream
}
- // send the standard headers
- // and the custom in MailMessage
- private IDictionary CreateHeaders( MailMessageWrapper msg ) {
- Hashtable headers = new Hashtable();
-
- headers[ "From" ] = msg.From.ToString();
- headers[ "To" ] = msg.To.ToString();
-
- if( msg.Cc.Count > 0 ) headers[ "Cc" ] = msg.Cc.ToString();
-
- if( msg.Bcc.Count > 0 ) headers[ "Bcc" ] = msg.Bcc.ToString();
-
- if( HasData( msg.Subject ) ) {
-
- // if the BodyEncoding is not 7bit us-ascii then
- // convert using base64
- if( msg.BodyEncoding is ASCIIEncoding ) {
-
- headers[ "Subject" ] = msg.Subject;
-
- } else {
-
- byte[] subjectBytes = msg.BodyEncoding.GetBytes( msg.Subject );
- // encode the subject with Base64
- headers[ "Subject" ] =
- String.Format( "=?{0}?{1}?{2}?=" ,
- msg.BodyEncoding.BodyName , "B",
- Convert.ToBase64String( subjectBytes ) );
- }
-
- }
-
- if( HasData( msg.UrlContentBase ) )
- headers[ "Content-Base" ] = msg.UrlContentBase;
-
- if( HasData( msg.UrlContentLocation ) )
- headers[ "Content-Location" ] = msg.UrlContentLocation;
-
-
- string charset = String.Format( "charset=\"{0}\"" , msg.BodyEncoding.BodyName );
-
- // set body the content type
- switch( msg.BodyFormat ) {
-
- case MailFormat.Html:
- headers[ "Content-Type" ] = "text/html; " + charset;
- break;
-
- case MailFormat.Text:
- headers[ "Content-Type" ] = "text/plain; " + charset;
- break;
-
- default:
- headers[ "Content-Type" ] = "text/plain; " + charset;
- break;
-
- }
-
- // set the priority as in the same way as .NET sdk does
- switch( msg.Priority ) {
-
- case MailPriority.High:
- headers[ "Importance" ] = "high";
- break;
-
- case MailPriority.Low:
- headers[ "Importance" ] = "low";
- break;
-
- case MailPriority.Normal:
- headers[ "Importance" ] = "normal";
- break;
-
- default:
- headers[ "Importance" ] = "normal";
- break;
-
- }
-
- // .NET sdk allways sets this to normal
- headers[ "Priority" ] = "normal";
-
-
- // add mime version
- headers[ "Mime-Version" ] = "1.0";
-
- // set the mailer -- should probably be changed
- headers[ "X-Mailer" ] = "Mono (System.Web.Mail.SmtpMail.Send)";
-
- // Set the transfer encoding.. it seems like only sends 7bit
- // if it is ASCII
- if( msg.BodyEncoding is ASCIIEncoding ) {
- headers[ "Content-Transfer-Encoding" ] = "7bit";
- } else {
- headers[ "Content-Transfer-Encoding" ] = "8bit";
- }
-
-
- // add the custom headers they will overwrite
- // the earlier ones if they are the same
- foreach( string key in msg.Headers.Keys )
- headers[ key ] = (string)msg.Headers[ key ];
-
-
-
- return headers;
- }
-
- // returns true if str is not null and not
- // empty
- private bool HasData( string str ) {
- bool hasData = false;
- if( str != null ) {
- if( str.Length > 0 ) {
- hasData = true;
- }
- }
- return hasData;
- }
-
-
// send quit command and
// closes the connection
public void Close() {
\r
using System;\r
using System.Net;\r
+using System.Text;\r
using System.IO;\r
using System.Reflection;\r
\r
\r
public static void Send (MailMessage message) \r
{\r
- \r
+ \r
+ // if no encoding is set then set the system\r
+ // default encoding\r
+ if( message.BodyEncoding == null ) message.BodyEncoding = Encoding.Default;\r
+ \r
try {\r
\r
MailMessageWrapper messageWrapper = new MailMessageWrapper( message );\r
}
- public void WriteHeaders( IDictionary headers ) {
+ public void WriteHeader( MailHeader header ) {
// write the headers
- foreach( string key in headers.Keys )
- WriteLine( "{0}: {1}" , key , (string)headers[ key ] );
+ foreach( string key in header.Data.AllKeys )
+ WriteLine( "{0}: {1}" , key , header.Data[ key ] );
// write the header end tag
WriteLine( "" );
System.Web.Mail/IAttachmentEncoder.cs
System.Web.Mail/Base64AttachmentEncoder.cs
System.Web.Mail/ToUUEncodingTransform.cs
+System.Web.Mail/MailHeader.cs
System.Web.UI/BuildMethod.cs
System.Web.UI/BuildTemplateMethod.cs
System.Web.UI/Control.cs