Click here to Skip to main content
11,575,921 members (57,372 online)
Click here to Skip to main content

End-to-end Email Address Verification for Applications

, 19 Mar 2013 CPOL 609.7K 6.7K 196
Rate this:
Please Sign up or sign in to vote.
In this article, we would discuss a very brief and overall technique to verify the email addresses of the users that signup for a web account.

 Introduction  

Email has become a necessary and inseparable part of our day-to-day life. Even in the web applications we develop, the primary mode of information exchange between the website/application and the user is the email address. For this, some sites have a primary email and a secondary email (if something fails in primary, the information to be communicated to the user would be sent to the secondary address).

In any web portal and/or applications, where a diversified set of users are expected to visit and register, care should be taken, in validating the email address, since this is being intended to serve as the primary medium of contact between the user and the website. 

Scope:  

The scope of this utility is two-pronged:

  1. Soft syntactical validation of email address. 
  2. Deep Network Checking where in the email server is contacted for the existence of the address. 

Validations 

A very preliminary validation of email addresses is by analyzing the pattern of addresses. That is absolutely straight forward and we can define a regular expression to get the job done.

The following regular expression method in C#, would tell you, if the passed email address is syntactically valid or not. Note that, this verifies only syntactical validity and not whether the email address exists or not.

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);
} 

The next level of validation, we can attempt is to make a negotiation with the SMTP server and validate. Some mail servers respond even to VRFY and/or RCPT SMTP commands as to whether the email address is valid or not. But servers which are very strictly configured of not disclosing non-existing addresses, will always acknowledge any junk addresses in their domain and would bounce them on their own later. We need to tackle each of the following.

Validating via SMTP Network Connection

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); 

This step will throw an exception, if the domain is not valid, so that you can flag that the email address is invalid.

Validating via SMTP Handshakes

If the domain was okay, we can try to handshake with the actual server and find out whether the email address is valid or not. Perhaps, at this point, I would like to suggest the way an application used to negotiate to a SMTP server similar to how Peter has explained here (EggHeadCafe). We would not need the entire block of code anyway. 

We can check each section of the SMTP negotiation like MAIL FROM and RCPT TO and optionally VRFY SMTP commands. 

If from domains or from addresses are prohibited or not in the SMTP server's allow list, MAIL FROM may fail. Mail servers which allow VRFY command will let you understand whether the email address is valid or not.

My CodeSection

Since we had a similar requirement, the EggHeadCafe was really useful and I would like to share the code snippet for other users, who might be having a similar requirement.

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", 
     "testexample@deepak.portland.co.uk"));
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);

Check_Response, SendData are available in the original source code and you can download it from there. But you may need to read through the associated license agreement, regarding retaining copyright notices in your code. Since this is just a code snippet to introduce you to the idea, only relevant code area are being mentioned.

Temporary Validation

All goes well, if network conditions are ok. But there may be temporary network problems preventing connections. If you expect that your host may be slow, then you can send a dummy link to the email address and activate the account only if the user goes to the address and clicks the link. Otherwise, you can stop the account activation step, periodically reclaiming junk accounts by having a scheduled task in your web application. 

DNS Utility: 

Sincere thanks and credit is given to Heijden whose DNS utility is being made use of for looking Mx servers in the application. This provides a cleaner separation of concerns in the application. 

To Summarize...

In fact, I hope a lot of web developers would be in need of similar validation routines, to ensure that the email addresses are valid and I really hope that the above hints would be helpful to them. Thanks Peter, your article really helped me and I hope your article and whatever hints I have been learning, which I have shared above, would really help more developers having similar requirements to solve. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vasudevan Deepak Kumar
Software Developer
India India
Vasudevan Deepak Kumar is from Chennai, India who has been in the programming career since 1994, when he was 15 years old. He has his Bachelors of Engineering (in Computer Science and Engineering) from Vellore Engineering College. He also has a MBA in Systems from Alagappa University, Karaikudi, India.
He started his programming career with GWBasic and then in his college was involved in developing programs in Fortran, Cobol, C++. He has been developing in Microsoft technologies like ASP, SQLServer 2000.
His current focus is ASP.NET, C#, VB.NET, PHP, SQL Server and MySQL. In his past-time, he listens to polite Carnatic Music. But the big question is that with his current Todolist backlog, does he get some past time?

You may also be interested in...

Comments and Discussions

 
QuestionGot It working - What's the risk Pin
Ray Causey7-Apr-15 15:15
memberRay Causey7-Apr-15 15:15 
QuestionThrowing an error when trying to Network Validate Pin
Member 1137222713-Jan-15 4:32
memberMember 1137222713-Jan-15 4:32 
AnswerRe: Throwing an error when trying to Network Validate Pin
BrianBalino15-Jan-15 12:24
memberBrianBalino15-Jan-15 12:24 
QuestionAnswer to not working issue Pin
BrianBalino11-Jan-15 19:14
memberBrianBalino11-Jan-15 19:14 
SuggestionGreat code unfinished though Pin
houman_gh8-Aug-14 5:13
memberhouman_gh8-Aug-14 5:13 
GeneralMy vote of 1 Pin
ali_331031-Jul-14 16:10
memberali_331031-Jul-14 16:10 
BugRe: My vote of 1 Pin
Vasudevan Deepak Kumar2-Apr-15 22:49
memberVasudevan Deepak Kumar2-Apr-15 22:49 
QuestionFail's for every E-Mail Address Pin
steve.davis-usa20-May-14 10:59
membersteve.davis-usa20-May-14 10:59 
AnswerRe: Fail's for every E-Mail Address Pin
amassio24-Jul-14 3:25
memberamassio24-Jul-14 3:25 
QuestionStatus return FALSE always? Pin
the_fate2-May-14 6:56
memberthe_fate2-May-14 6:56 
QuestionAlways got 501 response Pin
LaminLay3-Apr-14 19:53
memberLaminLay3-Apr-14 19:53 
QuestionNot working for all mails Pin
PraveenMalik23-Dec-13 22:23
memberPraveenMalik23-Dec-13 22:23 
QuestionRe: Not working for all mails Pin
Vasudevan Deepak Kumar24-Dec-13 1:26
memberVasudevan Deepak Kumar24-Dec-13 1:26 
AnswerRe: Not working for all mails Pin
PraveenMalik24-Dec-13 5:05
memberPraveenMalik24-Dec-13 5:05 
AnswerRe: Not working for all mails Pin
Vasudevan Deepak Kumar24-Dec-13 5:08
memberVasudevan Deepak Kumar24-Dec-13 5:08 
GeneralMy vote of 5 Pin
superbDotNetDeveloper21-Aug-13 23:58
membersuperbDotNetDeveloper21-Aug-13 23:58 
QuestionFails on all emails Pin
smiley silvia31-May-13 6:07
membersmiley silvia31-May-13 6:07 
SuggestionRe: Fails on all emails Pin
Vasudevan Deepak Kumar4-Jun-13 7:45
memberVasudevan Deepak Kumar4-Jun-13 7:45 
Questionshowing failed Pin
Kuldev20-Apr-13 10:01
memberKuldev20-Apr-13 10:01 
QuestionRe: showing failed Pin
Vasudevan Deepak Kumar24-Dec-13 1:27
memberVasudevan Deepak Kumar24-Dec-13 1:27 
QuestionFails on all email Pin
Ken Alexander3-Apr-13 11:45
memberKen Alexander3-Apr-13 11:45 
QuestionRe: Fails on all email Pin
Vasudevan Deepak Kumar3-Apr-13 12:25
memberVasudevan Deepak Kumar3-Apr-13 12:25 
AnswerRe: Fails on all email Pin
Ken Alexander4-Apr-13 6:01
memberKen Alexander4-Apr-13 6:01 
QuestionRe: Fails on all email Pin
Vasudevan Deepak Kumar4-Apr-13 6:26
memberVasudevan Deepak Kumar4-Apr-13 6:26 
AnswerRe: Fails on all email Pin
Ken Alexander4-Apr-13 6:36
memberKen Alexander4-Apr-13 6:36 
GeneralRe: Fails on all email Pin
Vasudevan Deepak Kumar4-Apr-13 6:50
memberVasudevan Deepak Kumar4-Apr-13 6:50 
GeneralRe: Fails on all email Pin
Ken Alexander4-Apr-13 7:23
memberKen Alexander4-Apr-13 7:23 
GeneralRe: Fails on all email Pin
Ken Alexander4-Apr-13 9:50
memberKen Alexander4-Apr-13 9:50 
QuestionRe: Fails on all email Pin
Vasudevan Deepak Kumar4-Apr-13 10:03
memberVasudevan Deepak Kumar4-Apr-13 10:03 
AnswerRe: Fails on all email Pin
Ken Alexander4-Apr-13 10:52
memberKen Alexander4-Apr-13 10:52 
AnswerRe: Fails on all email Pin
Ken Alexander9-Apr-13 5:56
memberKen Alexander9-Apr-13 5:56 
GeneralRe: Fails on all email Pin
Vasudevan Deepak Kumar9-Apr-13 5:58
memberVasudevan Deepak Kumar9-Apr-13 5:58 
GeneralRe: Fails on all email Pin
Ken Alexander9-Apr-13 6:02
memberKen Alexander9-Apr-13 6:02 
AnswerRe: Fails on all email Pin
Member 1084487926-May-14 6:13
memberMember 1084487926-May-14 6:13 
QuestionGetting 555 syntax error Pin
lakhan12330-Mar-13 1:02
memberlakhan12330-Mar-13 1:02 
AnswerRe: Getting 555 syntax error Pin
Vasudevan Deepak Kumar30-Mar-13 3:35
memberVasudevan Deepak Kumar30-Mar-13 3:35 
QuestionChange Prototype Pin
_alert_21-Mar-13 18:22
member_alert_21-Mar-13 18:22 
GeneralMy vote of 4 Pin
Prasad Khandekar20-Mar-13 22:15
memberPrasad Khandekar20-Mar-13 22:15 
GeneralMy vote of 5 Pin
gstolarov20-Mar-13 4:45
membergstolarov20-Mar-13 4:45 
GeneralRe: My vote of 5 Pin
superbDotNetDeveloper22-Aug-13 0:01
membersuperbDotNetDeveloper22-Aug-13 0:01 
SuggestionRegex Etc. Pin
Mike-MadBadger19-Mar-13 20:42
memberMike-MadBadger19-Mar-13 20:42 
QuestionPlease Help Pin
ali_331014-Mar-13 10:19
memberali_331014-Mar-13 10:19 
GeneralMy vote of 1 Pin
Raghu KT12-Sep-12 0:37
memberRaghu KT12-Sep-12 0:37 
GeneralNo download Pin
Raghu KT11-Sep-12 21:24
memberRaghu KT11-Sep-12 21:24 
GeneralMy vote of 1 Pin
Gourav Bhatt7-Jun-12 20:17
memberGourav Bhatt7-Jun-12 20:17 
GeneralMy vote of 5 Pin
Jack_32131-May-12 11:31
memberJack_32131-May-12 11:31 
QuestionError in attempting to connect Pin
Yenyen Suwardy8-May-12 22:02
memberYenyen Suwardy8-May-12 22:02 
AnswerRe: Error in attempting to connect - because the code is wrong Pin
maciekplewa18-Jul-12 19:32
membermaciekplewa18-Jul-12 19:32 
GeneralMy vote of 4 Pin
naveen kacham28-Feb-12 18:53
membernaveen kacham28-Feb-12 18:53 
Suggestion!Check Responce Code snippet Pin
nobuhle.mnguni@gmail.com28-Oct-11 0:28
membernobuhle.mnguni@gmail.com28-Oct-11 0:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 19 Mar 2013
Article Copyright 2003 by Vasudevan Deepak Kumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid