Email地址有效性验证

类别:Asp 点击:0 评论:0 推荐:

Email地址有效性的检验是一个经常遇到的问题啦!一般的检验方法是对Email地址字符串进行简单的检验,如是否含有@ .等有效字符等。这种方法只能保证该地址从格式上看似有效,并不能保证地址可达。最近进行大量的地址校验,写了一个小程序,可以保证Email地址真正可达。

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;[email protected]");
   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:[email protected]
>>250 Ok
RCPT TO:[email protected]
>>250 ok its for [email protected]
DATA
>>ok.send it ;end with <CRLF>.<CRLF>
soem data.
>>250 message queued
QUIT
>>221 Goodbye.

本文地址:http://com.8s8s.com/it/it8206.htm