Ask a questionAsk a question
 

Answervalidate an email address?

  • Monday, September 07, 2009 7:22 PMJassim Rahma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    how can I validate if the email address provided by the user does exists or not before proceeding to send an email using SmtpClient in C#?


    Jassim Rahma

Answers

All Replies

  • Tuesday, September 08, 2009 5:39 AMKhanna Gaurav Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    • Marked As Answer byJassim Rahma Tuesday, September 08, 2009 2:06 PM
    •  
  • Tuesday, September 08, 2009 9:08 AMRohini Chavakula Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Code snippet if the email address is like xxx_xxxx@xx.com:

            
    string pattern = @"[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]@[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]-[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].com";
    
    System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(editemail.Text.Trim(), pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
     if (!match.Success) 
    {
     MessageBox.Show("You must specify valid E-mail", this.vGlobalVariables.gProductVersion, MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     this.editemail.Focus();}
    

     General regex pattern related to your requirement can be found in http://www.regular-expressions.info/email.html

    Cheers:)
    Mark as answer if it helps to solve your query

  • Friday, November 06, 2009 3:33 PMEfran Cobisi [cobisi.com] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello Jassim,

    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.