Click here to Skip to main content
11,414,234 members (70,552 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
Add your own
alternative version

A C# Implementation of Mime De/encode

, 21 Aug 2005 CPOL
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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 22 Aug 2005
Article Copyright 2005 by tauyoung
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid