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

SMTP and POP3 Mail Server

Rate me:
Please Sign up or sign in to vote.
4.88/5 (96 votes)
29 Sep 20031 min read 1M   18.9K   315  
An SMTP and POP3 mail server written using the .NET Framework and C#.
using System;
using System.Text;

namespace LumiSoft.Net.Dns
{
	/// <summary>
	/// Summary description for Dns_Query.
	/// </summary>
	internal class Dns_Query
	{
		private string m_QNAME  = "";
		private QTYPE  m_QTYPE  = QTYPE.MX;
		private int    m_QCALSS = 1;

		/// <summary>
		/// Default constructor.
		/// </summary>
		public Dns_Query()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="qname"></param>
		/// <param name="qtype"></param>
		/// <param name="qclass"></param>
		public Dns_Query(string qname,QTYPE qtype,int qclass)
		{
			m_QNAME  = qname;
			m_QTYPE  = qtype;
			m_QCALSS = qclass;
		}

        
		#region function GetQuery

		/// <summary>
		/// Gets query.
		/// </summary>
		/// <param name="header"></param>
		/// <returns></returns>
		public byte[] GetQuery(Dns_Header header)
		{
			byte[] query = null;

			try
			{
				/* 	4.1.2. Question section format
					                          1  1  1  1  1  1
				0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
				+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
				|                                               |
				/                     QNAME                     /
				/                                               /
				+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
				|                     QTYPE                     |
				+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
				|                     QCLASS                    |
				+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
			
				QNAME
					a domain name represented as a sequence of labels, where
				 each label consists of a length octet followed by that
					number of octets.  The domain name terminates with the
				 zero length octet for the null label of the root.  Note
					that this field may be an odd number of octets; no
				 padding is used.
				*/

				query = new byte[521];

				// Copy dns header to query
				byte[] head = header.GetHeader();
				head.CopyTo(query,0);

				//--------- Query part ------------------------------------//
				string[] labels = m_QNAME.Split(new char[] {'.'});
				int position = 12;
					
				// Copy all domain parts(labels) to query
				// eg. lumisoft.ee = 2 labels, lumisoft and ee.
				// format = label.length + label(bytes)
				foreach(string label in labels){
					// add label lenght to query
					query[position++] = (byte)(label.Length); 

					// convert label string to byte array
					byte[] b = Encoding.ASCII.GetBytes(label.ToCharArray());
					b.CopyTo(query,position);

					// Move position by label length
					position += b.Length;
				}

				// Terminate domain (see note above)
				query[position++] = (byte) 0; 
			
				// Set QTYPE 
				query[position++] = (byte) 0;
				query[position++] = (byte)m_QTYPE;
				
				// Set QCLASS
				query[position++] = (byte) 0;
				query[position++] = (byte)m_QCALSS;
				//-------------------------------------------------------//
			}
			catch
			{
				query = null;
			}

			return query;
		}

		#endregion


		#region Properties Implementation

		/// <summary>
		/// 
		/// </summary>
		public string QNAME
		{
			get{ return m_QNAME; }
		}

		/// <summary>
		/// Gets query type.
		/// </summary>
		public QTYPE QTYPE
		{
			get{ return m_QTYPE; }
		}

		/// <summary>
		/// Gets query class.
		/// </summary>
		public int QCLASS
		{
			get{ return m_QCALSS; }
		}

		#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