Browse Source

Added classes:
SmtpMessage - A more sofisticated mail container than MailMessage
MailAddress - A class that represents a mail address
MailAddressCollection - A collection of mail adresses

Theese classes where added to support the funtionality of serveral
recievers and Cc and Bcc. And also to ensure the format of the
address strings.

svn path=/trunk/mcs/; revision=12230

Per Arneng 23 years ago
parent
commit
cb9e18ec9d

+ 94 - 0
mcs/class/System.Web/System.Web.Mail/MailAddress.cs

@@ -0,0 +1,94 @@
+using System;
+
+namespace System.Web.Mail {
+
+    internal class MailAddress {
+	
+	protected string user;
+	protected string host;
+	protected string name;
+	
+	public string User {
+	    get { return user; }
+	    set { user = value; }
+	}
+
+	public string Host {
+	    get { return host; }
+	    set { host = value; }
+	}
+
+	public string Name {
+	    get { return name; }
+	    set { name = value; }
+	}
+
+	public string Address {
+	    get { return String.Format( "{0}@{1}" , user , host ); }
+	    set {
+		
+		string[] parts = value.Split( new char[] { '@' } );
+		
+		if( parts.Length != 2 ) 
+		    throw new FormatException( "Email address is incorrect: " + value );
+		
+		user = parts[ 0 ];
+		host = parts[ 1 ];
+	    }
+	}
+
+	public static MailAddress Parse( string str ) {
+	    MailAddress addr = new MailAddress();
+	    string address = null;
+	    string nameString = null;
+	    string[] parts = str.Split( new char[] { ' ' } );
+	    
+	    // find the address: [email protected]
+	    // and put to gether all the parts
+	    // before the address as nameString
+	    foreach( string part in parts ) {
+		
+		if( part.IndexOf( '@' ) > 0 ) {
+		    address = part;
+		    break;
+		}
+		
+		nameString = nameString + part + " ";
+	    }
+
+	    if( address == null ) 
+		throw new FormatException( "Email address not found in: " + str );
+	    
+	    address = address.Trim( new char[] { '<' , '>' , '(' , ')' } );
+	    
+	    addr.Address = address;
+	    
+	    if( nameString != null ) {
+		addr.Name = nameString.Trim();
+		addr.Name = ( addr.Name.Length == 0 ? null : addr.Name ); 
+	    }
+	    
+	    
+	    return addr;
+	} 
+    
+    
+	public override string ToString() {
+	    
+	    string retString = "";
+	
+	    if( name == null ) {
+		
+		retString = String.Format( "<{0}>" , this.Address );
+	    
+	    } else {
+		
+		retString = String.Format( "\"{0}\" <{1}>" , this.Name , this.Address);
+	    
+	    }
+	    
+	    return retString;
+	}
+    }
+
+}

+ 56 - 0
mcs/class/System.Web/System.Web.Mail/MailAddressCollection.cs

@@ -0,0 +1,56 @@
+using System;
+using System.Text;
+using System.Collections;
+
+namespace System.Web.Mail {
+
+    internal class MailAddressCollection : IEnumerable {
+	
+	protected ArrayList data = new ArrayList();
+	
+	public MailAddress this[ int index ] {
+	    get { return this.Get( index ); }
+	}
+
+	public int Count { get { return data.Count; } }
+	
+	public void Add( MailAddress addr ) { data.Add( addr ); }
+	public MailAddress Get( int index ) { return (MailAddress)data[ index ]; }
+
+	public IEnumerator GetEnumerator() {
+	    return data.GetEnumerator();
+	}
+    
+    
+	public override string ToString() {
+	    
+	    StringBuilder builder = new StringBuilder();
+	    for( int i = 0; i <data.Count ; i++ ) {
+		MailAddress addr = this.Get( i );
+		
+		builder.Append( addr );
+		
+		if( i != ( data.Count - 1 ) ) builder.Append( ", " );
+	    }
+
+	    return builder.ToString(); 
+	}
+
+	public static MailAddressCollection Parse( string str ) {
+	    
+	    if( str == null ) throw new ArgumentNullException("Null is not allowed as an address string");
+	    
+	    MailAddressCollection list = new MailAddressCollection();
+	    
+	    string[] parts = str.Split( new char[] { ',' , ';' } );
+	    
+	    foreach( string part in parts ) {
+		list.Add( MailAddress.Parse( part ) );
+	    }
+	
+	    return list;
+	}
+	
+    }
+
+}

+ 26 - 14
mcs/class/System.Web/System.Web.Mail/SmtpClient.cs

@@ -43,10 +43,15 @@ namespace System.Web.Mail {
 	    	    
 	}
 	
-	public void Send( MailMessage msg ) {
+	public void Send( SmtpMessage msg ) {
+	    
+	    if( msg.From == null ) {
+		throw new SmtpException( "From property must be set." );
+	    }
 
-	    if( ( ! HasData( msg.From )  ) || ( ! HasData( msg.To ) ) )
-		throw new SmtpException( "From & To properties must be set." );
+	    if( msg.To == null ) {
+		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
@@ -58,10 +63,17 @@ namespace System.Web.Mail {
 	    smtp.WriteRset();
 	    
 	    // write the mail from command
-	    smtp.WriteMailFrom( msg.From );
-	    	    
-	    // write the rcpt to command
-	    smtp.WriteRcptTo( msg.To );
+	    smtp.WriteMailFrom( msg.From.Address );
+	    
+	    // write the rcpt to command for the To addresses
+	    foreach( MailAddress addr in msg.To ) {
+		smtp.WriteRcptTo( addr.Address );
+	    }
+
+	    // write the rcpt to command for the Cc addresses
+	    foreach( MailAddress addr in msg.Cc ) {
+		smtp.WriteRcptTo( addr.Address );
+	    }
 	    
 	    // write the data command and then
 	    // send the email
@@ -84,7 +96,7 @@ namespace System.Web.Mail {
 	}
 	
 	// sends a single part mail to the server
-	private void SendSinglepartMail( MailMessage msg ) {
+	private void SendSinglepartMail( SmtpMessage msg ) {
 	    	    	    
 	    // create the headers
 	    IDictionary headers = CreateHeaders( msg );
@@ -97,7 +109,7 @@ namespace System.Web.Mail {
 	}
 	
 	// sends a multipart mail to the server
-	private void SendMultipartMail( MailMessage msg ) {
+	private void SendMultipartMail( SmtpMessage msg ) {
 	    	    	    
 	    // create the headers
 	    IDictionary headers = CreateHeaders( msg );
@@ -177,15 +189,15 @@ namespace System.Web.Mail {
 	
 	// send the standard headers
 	// and the custom in MailMessage
-	private IDictionary CreateHeaders( MailMessage msg ) {
+	private IDictionary CreateHeaders( SmtpMessage msg ) {
 	    Hashtable headers = new Hashtable(); 
 	    
-	    headers[ "From" ] = msg.From;
-	    headers[ "To" ] = msg.To;
+	    headers[ "From" ] = msg.From.ToString();
+	    headers[ "To" ] = msg.To.ToString();
 	    	    
-	    if( HasData( msg.Cc ) ) headers[ "Cc" ] = msg.Cc;
+	    if( msg.Cc.Count > 0 ) headers[ "Cc" ] = msg.Cc.ToString();
 			    
-	    if( HasData( msg.Bcc ) ) headers[ "Bcc" ] = msg.Bcc;
+	    if( msg.Bcc.Count > 0 ) headers[ "Bcc" ] = msg.Bcc.ToString();
 	    
 	    if( HasData( msg.Subject ) ) {
 		

+ 28 - 2
mcs/class/System.Web/System.Web.Mail/SmtpMail.cs

@@ -34,12 +34,38 @@ namespace System.Web.Mail
 		
 		public static void Send (MailMessage message) 
 		{
-		 
+		    
+		    SmtpMessage msg = new SmtpMessage ();
+		    
+		    try {
+			
+			if( message.From != null ) msg.From = MailAddress.Parse (message.From);
+			if( message.To != null ) msg.To = MailAddressCollection.Parse (message.To);
+			if( message.Cc != null ) msg.Cc = MailAddressCollection.Parse (message.Cc);
+			if( message.Bcc != null ) msg.Bcc = MailAddressCollection.Parse (message.Bcc);
+			
+			msg.Headers = message.Headers;
+			msg.UrlContentBase = message.UrlContentBase;
+			msg.UrlContentLocation = message.UrlContentLocation;
+			msg.Priority = message.Priority;
+			msg.Subject = message.Subject;
+			
+			msg.Body = message.Body;
+			msg.BodyEncoding = message.BodyEncoding; 
+			msg.BodyFormat = message.BodyFormat;
+			
+			msg.Attachments = message.Attachments;   
+						
+		    } catch (FormatException ex) {
+			throw new HttpException (ex.Message);
+		    }
+		    
+		    
 		    try {
 			
 			SmtpClient smtp = new SmtpClient (smtpServer);
 			
-			smtp.Send (message);
+			smtp.Send (msg);
 			
 			smtp.Close ();
 		    

+ 101 - 0
mcs/class/System.Web/System.Web.Mail/SmtpMessage.cs

@@ -0,0 +1,101 @@
+using System;
+using System.Collections;
+using System.Text;
+
+namespace System.Web.Mail {
+
+    internal class SmtpMessage {
+	
+	private IList attachments;
+	private MailAddressCollection bcc;
+	private string body;
+	private Encoding bodyEncoding;
+	private MailFormat bodyFormat;
+	private MailAddressCollection cc;		
+	private MailAddress from;
+	private IDictionary headers;
+	private MailPriority priority;
+	private string subject;
+	private MailAddressCollection to;
+	private string urlContentBase;
+	private string urlContentLocation;
+		
+	// Constructor		
+	public SmtpMessage ()
+	{
+	    attachments = new ArrayList (8);
+	    headers = new Hashtable ();
+	    to = new MailAddressCollection();
+	    cc = new MailAddressCollection();
+	    bcc = new MailAddressCollection();
+	    
+	}		
+	
+	// Properties
+	public IList Attachments {
+	    get { return attachments; }
+	    set { attachments = value; }
+	}		
+		
+	public MailAddressCollection Bcc {
+	    get { return bcc; } 
+	    set { bcc = value; }
+	}
+	
+	public string Body {
+	    get { return body; } 
+	    set { body = value; }
+	}
+
+	public Encoding BodyEncoding {
+	    get { return bodyEncoding; } 
+	    set { bodyEncoding = value; }
+	}
+
+	public MailFormat BodyFormat {
+	    get { return bodyFormat; } 
+	    set { bodyFormat = value; }
+	}		
+
+	public MailAddressCollection Cc {
+	    get { return cc; } 
+	    set { cc = value; }
+	}
+
+	public MailAddress From {
+	    get { return from; } 
+	    set { from = value; }
+	}
+
+	public IDictionary Headers {
+	    get { return headers; }
+	    set { headers = value; }
+	}
+		
+	public MailPriority Priority {
+	    get { return priority; } 
+	    set { priority = value; }
+	}
+		
+	public string Subject {
+	    get { return subject; } 
+	    set { subject = value; }
+	}
+
+	public MailAddressCollection To {
+	    get { return to; }   
+	    set { to = value; }
+	}
+
+	public string UrlContentBase {
+	    get { return urlContentBase; } 
+	    set { urlContentBase = value; }
+	}
+
+	public string UrlContentLocation {
+	    get { return urlContentLocation; } 
+	    set { urlContentLocation = value; }
+	}
+    }
+
+}

+ 3 - 0
mcs/class/System.Web/list

@@ -99,6 +99,9 @@ System.Web.Mail/SmtpStream.cs
 System.Web.Mail/SmtpException.cs
 System.Web.Mail/SmtpResponse.cs
 System.Web.Mail/MailEncoder.cs
+System.Web.Mail/SmtpMessage.cs
+System.Web.Mail/MailAddress.cs
+System.Web.Mail/MailAddressCollection.cs
 System.Web.UI/BuildMethod.cs
 System.Web.UI/BuildTemplateMethod.cs
 System.Web.UI/Control.cs