article

Simple POP3 Console MailChecker C# class

Email
Submitted on: 1/1/2015 1:55:00 AM
By: Peter V. (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: C#
Views: 1458
 
     This example below is a very simple class that connect to you're isp provider and looks for mail. This class shows how to use the TCPClient class, how to create a new Thread , how to capture events (AsyncCallBack) from recieving data on the socket.

 
				
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
namespace myMail
{
	/// <summary>
	/// POP mail Retrieve 
	/// </summary>
	public class clsPOP
	{
		private TcpClient myPOP;
		private NetworkStream networkStream;
		private byte[] bytes;
		private string mTxtPassword;
		private string mTxtUsername;
		private string mTxtPopServer;
		private ConnState ConnectionState;
		private bool blnClosePop;
		private bool blnExitPop;
		private enum ConnState
		{
			Connect = 0,
			User= 1,
			Passw= 2,
			Stat= 3,
			List= 4,
			Mail= 5,
			Quit= 6,
		}
		public clsPOP()
		{
			myPOP = new TcpClient();
			mTxtPopServer = "Unknown";
			mTxtPassword = "Unknown";
			mTxtUsername = "Unknown";
			blnExitPop = false;
			bytes = new byte[myPOP.ReceiveBufferSize];
			Thread.AllocateDataSlot();
			Console.WriteLine("P O P 3 M a i l C h e c kV 1 . 0 ");
			Console.WriteLine("______________________________________");
		}
		
		public string Password
		{
			set
			{
				mTxtPassword = value;
			}
			get
			{
				return mTxtPassword;
			}
		}
		
		public string UserName
		{
			set
			{
				mTxtUsername = value;
			}
			get
			{
				return mTxtUsername;
			}
		}
		public string PopServer
		{
			set
			{
				mTxtPopServer = value;
			}
			get
			{
				return mTxtPopServer;
			}
		}
		private string GetTCPData()
		{
			int NumBytesRecieved;
			try
			{
				NumBytesRecieved = 
					networkStream.Read(bytes, 0, (int) myPOP.ReceiveBufferSize);
				return (Encoding.ASCII.GetString(bytes).Substring(0,NumBytesRecieved));
			}
			catch (Exception e )
			{
				return(e.ToString());
			}
		}
		// Event when data is recieved from server
		public void TcpDataRecieve( IAsyncResult ar )
		{
			int BeginPos;
			int EndPos;
			if (ConnectionState != ConnState.Quit)
			{
				Console.WriteLine("{0}" , GetTCPData());
			}
			else
			{
				BeginPos = String.Compare(GetTCPData(),"Subject");
			}
			MailChecking();
			networkStream.Flush();
			networkStream.BeginRead(bytes,1,1,new AsyncCallback( TcpDataRecieve ), myPOP);
		}
		private void MailChecking()
		{
			string Ret;
			switch(ConnectionState)
			{
				case ConnState.Connect :
					Ret = "USER " + mTxtUsername + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.User;
					break;
				case ConnState.User :
					Ret = "PASS " + mTxtPassword + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.Passw;
					break;
				case ConnState.Passw :
					Ret = "STAT " + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.Stat;
					break;
				case ConnState.Stat :
					Ret = "LIST " + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.Mail;
					break;
				case ConnState.Mail :
					Ret = "TOP 1 1 " + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.List;
					break;
				case ConnState.List :
					Ret = "QUIT " + '\n'; 
					networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
					ConnectionState = ConnState.Quit;
					break;
				case ConnState.Quit :
					myPOP.Close();
					blnClosePop = true;
					break;
			}
		}
		public void Connect()
		{
			try
			{
				blnClosePop = false;
				Console.WriteLine(".Connecting to : {0}", mTxtPopServer); 
				myPOP.Connect(PopServer,110);
				ConnectionState = ConnState.Connect;
				networkStream = myPOP.GetStream();
				networkStream.BeginRead(bytes,1,1,new AsyncCallback( TcpDataRecieve ), myPOP);
				
				while (!blnClosePop && !blnExitPop) 
				{
					Thread.Sleep(100);
				}
			}
			catch (Exception e )
			{
				Console.WriteLine ("Error Connecting {0}" , e.ToString());
				myPOP.Close();
			}
		}
		public void Close()
		{
			myPOP.Close();
			blnExitPop = true;
		}
		public void ThreadHandler()
		{
			Thread currentThread = Thread.CurrentThread;
			this.Connect();
		}
	}
}

Main Class , that use this class above


static void Main(string[] args)
{
clsPop myPop = new clsPOP();
myPop.UserName = "your username";
myPop.Password = "your password";
myPop.PopServer = "your isp pop server";
ThreadStart PopMail = new ThreadStart(myPop.ThreadHandler);
myPopMail = new Thread(PopMail);
myPopMail.Start();
while(myPopMail.IsAlive)
{
Thread.Sleep(100);
}
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.