最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Email地址有效性验证
时间:2022-06-30 11:34:07 编辑:袖梨 来源:一聚教程网
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
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.
soem data.
>>250 message queued
QUIT
>>221 Goodbye.
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
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
soem data.
>>250 message queued
QUIT
>>221 Goodbye.
相关文章
- 口袋斗罗大陆小舞有哪些玩法技巧 09-16
- 逆战未来冰狙击步枪如何组装 09-16
- 如鸢董奉角色怎么样 09-16
- 环外技能怎么进阶 09-16
- 三国群英传策定九州如何发展建筑 09-16
- 三国望神州郭嘉怎么玩 09-16