README.md

December 4, 2022 ยท View on GitHub

Simple POP3 Console MailChecker C# class

Description

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.

More Info

Submitted On
ByPeter V.
LevelBeginner
User Rating4.4 (31 globes from 7 users)
CompatibilityC#
CategoryInternet/ Browsers/ HTML
World.Net (C#, VB.net)
Archive File

Source Code

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 k  V 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);
}
}