最新下载
热门教程
- 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.
相关文章
- Metaplanet最新购入1,112枚比特币后总持仓突破1万枚大关 06-16
- 《艾塔纪元》奥莉维亚凯撒怎么样 06-16
- 安全可靠2025全球交易所虚拟币交易前十-最方便数字资产交易所币安推荐 06-16
- 《超能力冲刺》水风阵容怎么配队 06-16
- 《晶核》平民职业选择推荐细节解读 06-16
- 2025盘点正规币圈十大比特币交易平台TOP10-去中心化加密货币交易所币安app推荐 06-16