Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

A C# Implementation of Mime De/encode

Rate me:
Please Sign up or sign in to vote.
4.00/5 (16 votes)
21 Aug 2005CPOL 150.8K   4.2K   38  
A C# implementation of Mime de/encode
using System;
using System.Text;

namespace MIME
{
	/// <summary>
	/// 
	/// </summary>
	public class MimeField
	{
		public MimeField()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		private string m_strName;				// field name
		private string m_strValue;				// field value
		private string m_strCharset;			// charset for non-ascii text

		public void SetName(string pszName) { m_strName = pszName; }
		public string GetName() { return m_strName; }
		public void SetValue(string pszValue) { m_strValue = pszValue; }
		public string GetValue() { return m_strValue; }
		public void SetCharset(string pszCharset) { m_strCharset = pszCharset; }
		public string GetCharset() { return m_strCharset; }

		//set field parameter
		public void SetParameter(string pszAttr, string pszValue)
		{
			string newValue = pszAttr + "=" + pszValue;
			string[] strparams = m_strValue.Split(';');
			bool hasAttr = false;
			int i=0;
			if(strparams != null)
			{
				for(; i < strparams.Length ; i++)
				{
					int index = strparams[i].IndexOf(pszAttr, 0);
					if(index != -1)
					{
						hasAttr = true;
						break;
					}
				}
			}
			if(hasAttr)
			{
				m_strValue.Replace(strparams[i],newValue);
			}
			else
			{
				m_strValue += ";" + newValue;
			}
		}

		//get field parameter
		public string GetParameter(string pszAttr)
		{
			string[] strparams = m_strValue.Split(';');
			if(strparams != null)
			{
				for(int i=0; i < strparams.Length ; i++)
				{
					int index = strparams[i].IndexOf(pszAttr, 0);
					if(index != -1)
					{
						int begin = strparams[i].IndexOf('=');
						int end;
						if(strparams[i][begin+1] == '"')
						{
							begin += 2;
							end = strparams[i].IndexOf('"', begin);
						}
						else
						{
							begin += 1;
							end = strparams[i].IndexOf('\r', begin);
							if(end == -1) end = strparams[i].Length;
						}

						return strparams[i].Substring(begin, end-begin);
					}
				}
			}
			return null;
		}

		//clear field content
		public void Clear() { m_strName = ""; m_strValue = ""; m_strCharset = ""; }


		//load a field from a string buffer
		public void LoadField(string strData)
		{
			if(strData == null)
				throw new ArgumentNullException();

			int colonIndex = strData.IndexOf(':');
			if(colonIndex != -1)
			{
				m_strName = strData.Substring(0, colonIndex);
			}
			else
			{
				m_strName = "";
			}

			colonIndex++;
			m_strValue = strData.Substring(colonIndex, strData.Length-colonIndex).Trim();
			//need decode!!! and m_strCharset
			MimeCode aFieldCode = MimeCodeManager.Instance.GetCode(GetName());
			if(aFieldCode != null)
			{
				m_strValue = aFieldCode.DecodeToString(m_strValue);
				m_strCharset = aFieldCode.Charset;
			}
		}

		//store a field content to a string buffer
		public void Store(StringBuilder sb)
		{
			if(sb == null)
				throw new ArgumentNullException();

			MimeCode aFieldCode = MimeCodeManager.Instance.GetCode(GetName());
			if(aFieldCode != null)
			{
				aFieldCode.Charset = m_strCharset;
				sb.AppendFormat("{0}: {1}\r\n", m_strName, aFieldCode.EncodeFromString(m_strValue));
			}
			else
			{
				sb.AppendFormat("{0}: {1}\r\n", m_strName, m_strValue);
			}
		}

	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
I think I have to start coding my life now...Smile | :)

Comments and Discussions