Static bool ValidateEmailAccount (string server, int port, string userName, string password, out string ErrorMessage)
{
ErrorMessage = "";
// Create a tcp connection
TcpClient _ server = new TcpClient (server, port );
// Prepare
NetworkStream netStream = _ server. GetStream ();
StreamReader reader = new StreamReader (_ server. GetStream ());
If (! Reader. ReadLine (). Contains ("+ OK "))
{
// Failed
ErrorMessage = "server link failed ";
Return false;
}
String data;
Byte [] charData;
String CRLF = "\ r \ n ";
// Login
Data = "USER" + userName + CRLF;
CharData = System. Text. Encoding. ASCII. GetBytes (data );
NetStream. Write (charData, 0, charData. Length );
If (! Reader. ReadLine (). Contains ("+ OK "))
{
// Account Error
ErrorMessage = "Account Error ";
Return false;
}
Data = "PASS" + password + CRLF;
CharData = System. Text. Encoding. ASCII. GetBytes (data );
NetStream. Write (charData, 0, charData. Length );
If (! Reader. ReadLine (). Contains ("+ OK "))
{
// Incorrect password
ErrorMessage = "Incorrect password ";
Return false;
}
Return true;
}
Call
String errorMessage;
Bool isContains = ValidateEmailAccount ("pop3.163.com", 110, "wise_sandy@XXX.com", "************", out errorMessage );
Console. WriteLine (isContains? "User exists": errorMessage );
From sandy's column