程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#驗證郵件的正則表達式的代碼

C#驗證郵件的正則表達式的代碼

編輯:關於C#

驗證輸入的正確性

public static bool isEmail( string inputEmail )
{
  inputEmail = NulltoString( inputEmail );
  string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
  Regex re = new Regex( strRegex );
  if ( re.IsMatch( inputEmail ) )
  return ( true );
  else
  return ( false );
}

驗證郵件地址的正確性:

string[] host = ( address.Split( @ ) );
string hostname = host[1];
IPHostEntry IPhst = Dns.Resolve( hostname );
IPEndPoint endPt = new IPEndPoint( IPhst.AddressList[0], 25 );
Socket s= new Socket( endPt.AddressFamily, SocketType.Stream,ProtocolType.Tcp );
s.Connect( endPt );
//Attempting to connect
if( !Check_Response( s, SMTPResponse.CONNECT_SUCCESS ) )
{
  s.Close( );
  return false;
}
//HELO server
Senddata( s, string.Format( "HELO {0}\r\n", Dns.GetHostName( )) );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{
  s.Close( );
  return false;
}
//Identify yourself
//Servers may resolve your domain and check whether you are listed in BlackLists etc.
Senddata( s, string.Format( "MAIL From: {0}\r\n","[email protected]" ) );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{
  s.Close( );
  return false;
}
//Attempt Delivery ( I can use VRFY, but most SMTP servers only disable it for security reasons )
Senddata( s, address );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{
  s.Close( );
  return false;
}
return ( true );

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved