public bool checkEmail(string mailAddress)
{
TcpClient tcpc=new TcpClient();
try{
string server=mailAddress.Split(@#@@#)[1];
tcpc.Connect(server,25);
NetworkStream s=tcpc.GetStream();
StreamReader sr=new StreamReader(s,Encoding.Default);
string strR="";
strR=sr.ReadLine();
if(!strR.StartsWith("220")) return false;
StreamWriter sw=new StreamWriter(s,Encoding.Default);
sw.WriteLine("HELO");
sw.Flush();
strR=sr.ReadLine();
if(!strR.StartsWith("250")) return false;
sw.WriteLine("MAIL FROM;brookes@tsinghua.org.cn");
sw.Flush();
strR=sr.ReadLine();
if(!strR.StartsWith("250")) return false;
sw.WriteLine("RCPT TO:"+mailAddress);
sw.Flush();
strR=sr.ReadLine();
if(!strR.StartsWith("250")) return false;
sw.WriteLine("QUIT");
sw.Flush();
strR=sr.ReadLine();
return true;
}catch(Exception ee)
{
return false;
}
}
这个程序是根据SMTP的基本过程实现的。与一个mail服务器连接发邮件的基本过程可能是这样的:
telnet mail.brookes.com 25
>>220 brookes.com<IMail 8.02>
HELO
>>250 mail.brookes.com
MAIL FROM:brookes@tsinghua.org.cn
>>250 Ok
RCPT TO:me@brookes.com
>>250 ok its for me@brookes.com
DATA
>>ok.send it ;end with <CRLF>.<CRLF>
soem data.
>>250 message queued
QUIT
>>221 Goodbye.
文章来源于领测软件测试网 https://www.ltesting.net/