最新下载
热门教程
- 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.
相关文章
- 哈利波特魔法觉醒拼图寻宝格兰芬多塔楼线索位置攻略 08-01
- 金铲铲之战中秋挑战活动怎么玩?中秋双人大作战玩法介绍 08-01
- 《魔法少女小圆MagiaExedra》月卡可获取奖励一览 08-01
- 《地下城堡4》军团战通关攻略 08-01
- 《地下城堡4》惊悸荒野小屋解密攻略 08-01
- 币圈三大主流交易所详解 币安 Coinbase 火币对比分析 08-01