Exemple #1
0
	public static MailAddress Parse( string str ) {
	    if (str == null || str.Trim () == "")
	    	return null;

	    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( "Invalid e-mail address: '" + str + "'.");
	    
	    address = address.Trim( new char[] { '<' , '>' , '(' , ')' } );
	    
	    addr.Address = address;
	    
	    if( nameString != null ) {
		addr.Name = nameString.Trim( new char[] { ' ' , '"' } );
		addr.Name = ( addr.Name.Length == 0 ? null : addr.Name ); 
	    }
	    
	    
	    return addr;
	} 
	// Constructor		
	public MailMessageWrapper( MailMessage message )
	{
	    this.message = message;
	    
	    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 ) {
		
		// encode the subject if it needs encoding
		if( MailUtil.NeedEncoding( message.Subject ) ) {
		    		
		    byte[] subjectBytes = message.BodyEncoding.GetBytes( message.Subject );
		    // encode the subject with Base64
		    header.Subject = "=?" + message.BodyEncoding.BodyName + "?B?" + Convert.ToBase64String (subjectBytes) + "?=";
		} else {
		    
		    header.Subject = message.Subject;
		
		}
	    }

	    // convert single '.' on a line with ".." to not
	    // confuse the smtp server since the DATA command
	    // is terminated with a '.' on a single line.
	    // this is also according to the smtp specs.
	    if( message.Body != null ) {
		body = message.Body.Replace( "\n.\n" , "\n..\n" );
		body = body.Replace( "\r\n.\r\n" , "\r\n..\r\n" );
	    }
	    
	    
	    // 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.Concat ( "text/html; charset=\"", message.BodyEncoding.BodyName, "\""); 
			    break;
	    
		    case MailFormat.Text: 
			    header.ContentType = String.Concat ( "text/plain; charset=\"", message.BodyEncoding.BodyName, "\"");
			    break;
	    
		    default: 
			    header.ContentType = String.Concat ( "text/html; charset=\"", 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 Date header, we were missing earlier 27/08/04
	    // RFC822 requires date to be in format Fri, 27 Aug 2004 20:13:20 +0530
	    //DateTime.Now gives in format 8/27/2004 8:13:00 PM
	    // Need to explore further dateTime formats available or do we need
	    // to write a function to convert.
		//header.Data.Add ("Date", DateTime.Now.ToString()); 

	    // Add the custom headers
	    foreach( string key in message.Headers.Keys )
		header.Data[ key ] = (string)this.message.Headers[ key ];
	}		
	public void Add( MailAddress addr ) { data.Add( addr ); }