Ask a questionAsk a question
 

Answervalidating an Email ID?

Answers

  • Tuesday, August 21, 2007 8:20 AMSibin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can use the following code for validating an email through C#


     public static Boolean IsValidEmail(string email)
            {
                try
                {
                    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})(\]?)$";
                    
                    System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex(strRegex);
                    System.Text.RegularExpressions.Match m;
                    m = exp.Match(email, 0);
                    return m.Success;
                }
  • Tuesday, August 21, 2007 12:17 PMBulldog.NET Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Additional Information

     

    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 SMTP Network Connection

    Code Snippet
    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.

  • Tuesday, August 21, 2007 2:22 PMtimvw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    And what if the MX record points to a different host? Wink

     (Anyway, as posted already, more details regarding 'validation' are required)


    Btw, coincidentally i read http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx today... Wink

All Replies

  • Tuesday, August 21, 2007 8:20 AMSibin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can use the following code for validating an email through C#


     public static Boolean IsValidEmail(string email)
            {
                try
                {
                    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})(\]?)$";
                    
                    System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex(strRegex);
                    System.Text.RegularExpressions.Match m;
                    m = exp.Match(email, 0);
                    return m.Success;
                }
  • Tuesday, August 21, 2007 10:57 AMtimvw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How would you define validating?

    - Does it have to mach a certain format? (Search the web for RFC822 compliant regex)
    - Does the have to be deliverable?
    - Has the user that provided the address to confirm that he actually is able to read incoming messages?
  • Tuesday, August 21, 2007 12:10 PMBulldog.NET Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Hi

     

    The best way is to use regex, see below

     

    Code Snippet

            using System.Text.RegularExpressions;

     

            private void cmdValidateEmail_Click(object sender, EventArgs e)

            {

                if (isValidEmail(txtEmail.Text))

                {

                    MessageBox.Show("Valid Email");

                }

                else

                {

                    MessageBox.Show("Invalid Email");

                }

            }

     

            public static bool isValidEmail(string 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);

            }

     

     

    Hope this helps.
  • Tuesday, August 21, 2007 12:17 PMBulldog.NET Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Additional Information

     

    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 SMTP Network Connection

    Code Snippet
    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.

  • Tuesday, August 21, 2007 2:22 PMtimvw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    And what if the MX record points to a different host? Wink

     (Anyway, as posted already, more details regarding 'validation' are required)


    Btw, coincidentally i read http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx today... Wink
  • Friday, November 06, 2009 3:46 PMEfran Cobisi [cobisi.com] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    In my company we use EmailVerify.NET (http://www.emailverify.net), a Microsoft .NET component which supports e-mail syntax checking, domain MX test and mailbox validation too.
    E-mail validation up to the mailbox level is easy as writing the following code block:

    var verifier = new EmailVerifier();
    var result = verifier.Verify("me@example.com", VerificationLevel.Mailbox);
    
    if (result.IsSuccess)
    {
       // Go on, send the message
    }
    

    Additional code samples can be found here.
    From their home page:

    EmailVerify.NET is a powerful .NET component which verifies email addresses using various techniques, including:
    • Syntax verification, according to RFC 2821 and RFC 2822
    • MX record lookup
    • Disposable email address (DEA) validation
    • SMTP availability check
    • Mailbox existence check, with greylisting support
    • Catch-all test

    It is completely written in managed code (C#) and is compliant with the Common Language Specification (CLS), so it can be used with any other .NET language, including VB.NET, C++/CLI, J#, IronPython, IronRuby and F#.

    The price for this component is very small (less than 50 bucks) and their support is one of the greatest I've ever found for a small software company like they are.
    Hope this helps.