Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C#

Advanced MIME Parser/Creator/Editor

Rate me:
Please Sign up or sign in to vote.
4.91/5 (66 votes)
5 Oct 2005 1M   7.7K   116  
An advanced MIME parser/creator/editor application.
using System;

namespace LumiSoft.Net.Mime
{
	/// <summary>
	/// Mime entity header field.
	/// </summary>
	public class HeaderField
	{
		private string m_Name  = "";
		private string m_Value = "";

		/// <summary>
		/// Default construtor.
		/// </summary>
		public HeaderField()
		{			
		}

		/// <summary>
		/// Creates new header field with specified name and value.
		/// </summary>
		/// <param name="name">Header field name. Header field name must end with colon(:) and may contain US-ASCII character values between 33 and 126.</param>
		/// <param name="value">Header field value.</param>
		public HeaderField(string name,string value)
		{
			this.Name  = name;
			this.Value = value;
		}


		#region Properties Implementation

		/// <summary>
		/// Gets or sets header field name. Header field name must end with colon(:) and may contain US-ASCII character values between 33 and 126.
		/// </summary>
		public string Name
		{
			get{ return m_Name; }

			set{ 
				/* RFC 2822 2.2 Header Fields
					Header fields are lines composed of a field name, followed by a colon
					(":"), followed by a field body, and terminated by CRLF.  A field
					name MUST be composed of printable US-ASCII characters (i.e.,
					characters that have values between 33 and 126, inclusive), except
					colon.  A field body may be composed of any US-ASCII characters,
					except for CR and LF.  However, a field body may contain CRLF when
					used in header "folding" and  "unfolding" as described in section
					2.2.3.  All field bodies MUST conform to the syntax described in
					sections 3 and 4 of this standard.
				*/

				if(value == ""){
					throw new Exception("Header Field name can't be empty !");
				}
				
				// Colon is missing from name, just add it
				if(!value.EndsWith(":")){
					value += ":";
				}

				// Check if name is between ASCII 33 - 126 characters
				foreach(char c in value.Substring(0,value.Length - 1)){
					if(c < 33 || c > 126){
						throw new Exception("Invalid field name '" + value + "'. A field name MUST be composed of printable US-ASCII characters (i.e.,characters that have values between 33 and 126, inclusive),except	colon.");
					}
				}

				m_Name = value; 
			}
		}

		/// <summary>
		/// Gets or sets header field value.
		/// </summary>
		public string Value
		{
			get{ return m_Value; }

			set{ m_Value = value; }
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions