Developer Geeks Home

How to verify the email address is valid in ASP.NET

RSS
 

  • Posted one year ago
  • Category: .Net Framework, ASP .Net,
  • Audiences: Architect, Developer, System Analyst,

This article will demonstrate how to verify that the email address provided by a user is a valid email address, using a sample code.

Introduction

Some times website owners need to allow users to get registered on their website or subscribe to newsletters on their website, by allowing users to provide their email addresses. But how would you verify that the user provided email addresses are genuine and valid, to avoid bounced email messages.

This article will demonstrate how to verify that the email address provided by a user is a valid email address, using a sample code.

Implementation

ASP.NET Provides Built-in DNS Resolving Support. DNS class from System.Net namespace can be used to resolve DNS hostnames into IP addresses. Using this class we could verify email addresses and ensure that the domain name specified by the user actually resolved to an existing domain name.

Sample Code

Following is the sample code that uses the DNS classes and its Resolve method, along with a regular expression, to build a fairly reliable email address validation technique in ASP.NET.

 <%@ Import Namespace=”System.Net” %>
 <%@ Import Namespace=”System.Net.Sockets” %>
 <%@ Import Namespace=”System.Text.RegularExpressions” %>
 <script language=”VB” runat=”server”>

Sub btnSubmit_OnClick(ByVal source As Object, ByVal e As EventArgs)
    'Check to make sure that the email addy is in the right form
    Dim strEmail As String, strPattern As String
    strEmail = txtEmail.Text
    strPattern = "^[\w-_\.]+\@([\w]+\.)+\w+$"

    If Not Regex.IsMatch(strEmail, strPattern, "i") Then
        'Invalid email address form!
        Response.Write("<font color=red><i>Your email address is in an" & _
        " illegal format.</i></font><p>")
    Else
        'Check to see if the domain name entered in the email address exists
        Dim strDomain As String
        strDomain = strEmail.Substring(strEmail.IndexOf("@") + 1)

        'Attempt to Resolve the hostname
        Dim strIP As String
        Try
            strIP = DNS.Resolve(strDomain).ToString()

            'If we reach here, we have a valid email address, so do whatever
            'processing or whatnot needs to be done...
            Response.Write("<b>Valid email address. Your domain name has " & _
            "an IP of " & strIP & ". Thank you!</b>")
        Catch se As SocketException
            'The DNS resolve was unsuccessful...
            strIP = se.Message

            Response.Write("<font color=red><i>" & strIP & "</i></font><p>")
        End Try
    End If
End Sub
 </script>

 <html>
     <body>
         <form runat=”server”>
             <b>Enter your Email address:</b>
             <asp:textbox id=”txtEmail” runat=”server” />
             </ br>
             <asp:button id=”btnSubmit” runat=”server” Text=”Check Email” OnClick=”btnSubmit_OnClick” />
         </form>
     </body>
 </html>

Additionally, I would recommend that you force user to respond to a confirmation email message sent to his provided email address, to guarantee that the user has provided a valid email address.

comments powered by Developer Geeks

My Recent Articles [86]