C#通过POP3协议验证 Email 账号
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"))
{
//失败
ErrorMessage = "server链接失败";
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"))
{
//账户错误
ErrorMessage = "账户错误";
return false;
}
data = "PASS " + password + CRLF;
charData = System.Text.Encoding.ASCII.GetBytes(data);
netStream.Write(charData, 0, charData.Length);
if (!reader.ReadLine().Contains("+OK"))
{
//密码错误
ErrorMessage = "密码错误";
return false;
}
return true;
}
调用
string errorMessage;
bool isContains = ValidateEmailAccount("pop3.163.com", 110, "wise_sandy@XXX.com", "************", out errorMessage);
Console.WriteLine(isContains ? "用户存在" : errorMessage);
摘自 sandy的专栏
补充:软件开发 , C# ,