Simple POP3 Console MailChecker C# class
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.
AI
Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Zdrojový kód
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 BGCOLOR=#FFFFFF>
<TR><TD>
<TT><PRE>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 + <B>'\n'; </B>
networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
ConnectionState = ConnState.User;
break;
case ConnState.User :
Ret = "PASS " + mTxtPassword + <B>'\n'; </B>
networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
ConnectionState = ConnState.Passw;
break;
case ConnState.Passw :
Ret = "STAT " + <B>'\n'; </B>
networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
ConnectionState = ConnState.Stat;
break;
case ConnState.Stat :
Ret = "LIST " + <B>'\n'; </B>
networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
ConnectionState = ConnState.Mail;
break;
case ConnState.Mail :
Ret = "TOP 1 1 " + <B>'\n'; </B>
networkStream.Write(System.Text.Encoding.ASCII.GetBytes(Ret),0, Ret.Length);
ConnectionState = ConnState.List;
break;
case ConnState.List :
Ret = "QUIT " + <B>'\n'; </B>
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();
}
}
}</PRE></TT>
</TD></TR>
</TABLE>
<H4>Main Class , that use this class above</H4><BR>
<H5>
static void Main(string[] args)<BR>
{<BR>
clsPop myPop = new clsPOP();<BR>
myPop.UserName = "your username";<BR>
myPop.Password = "your password";<BR>
myPop.PopServer = "your isp pop server";<BR>
ThreadStart PopMail = new ThreadStart(myPop.ThreadHandler);<BR>
myPopMail = new Thread(PopMail);<BR>
myPopMail.Start();<BR>
while(myPopMail.IsAlive)<BR>
{<BR>
Thread.Sleep(100);<BR>
}<BR>
}<BR></H5>
Původní komentáře (3)
Obnoveno z Wayback Machine