Ask a questionAsk a question
 

QuestionEmail-Validation using C#.net

  • Wednesday, April 30, 2008 1:39 PMDivakar_ibpm Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Hi Friends,

     

                   How to validate an Such an Email-id : test.test-test@test-med.com [One of my customer having such an mail-id]

                   Im validating using regular expression  in C#.net:

                    

                  

    string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +

    @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*([-][a-z|0-9]+)*\.([a-z]" +

    @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

     

     

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

    }

     

    While using the above code its saying You must specify valid email.

    How to validate the above email-id [test.test-test@test-med.com]?

     

     

    Thanks

    Divakar

     

     

All Replies

  • Wednesday, April 30, 2008 3:39 PMSheng Jiang 蒋晟MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Send an email to the address and have the user click a link to verify ownership of the email.
  • Thursday, May 01, 2008 12:14 AMLucian Baciu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    use

    Code Snippet

    string pattern = @"[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]@test-med.com";

     

     

    if the address will terminate in @test-med.com

     

    or

    Code Snippet

     

    string pattern2 = @"[-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";

     

     

    if the address will be something like ***-***@***-***.com

     

     

    i hope you got my point.

  • Thursday, May 01, 2008 5:21 AMDivakar_ibpm Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for ur valuable code...

     

    Thanks

    Divakar

     

  • Thursday, May 01, 2008 1:15 PMLucian Baciu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    glad i could be of help
  • Friday, May 02, 2008 1:25 PMJohn DeSouza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    You may find it useful to understand the logic behind the regex.

     

    http://www.regular-expressions.info/email.html

     

  • Monday, September 15, 2008 11:45 AMramesh.v Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi,


                   This is good sample


    regards,
    ramesh.v 
  • Tuesday, July 21, 2009 10:41 AMbalamurugan kalyanasundaram Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    hi,


          I would like to validate domain ID given in the txt_email (Textbox), if the valid domain id is given, i will let the user to submit the form otherwise not...

    can anyone help me asap, thanks in advance.

    This is my code:-

    public static bool IsDomainExistence(string strEmailID)
    {
    try
    {
    string[] host = (strEmailID.Split('@'));
    string hostname = host[1];

    IPHostEntry IPhst = Dns.GetHostEntry(hostname);
    IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
    Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    s.Connect(endPt);
    }
    catch (Exception)
    {
    return false;
    }
    return true;
    }


    It works fine in local, but when it is uploaded to server, it produces error message, even-if our domain id is given.


    regards,
    Balamurugan
  • Tuesday, July 21, 2009 12:46 PMJohnGrove Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Original posting --> Wednesday, April 30, 2008 1:39 PM
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Saturday, October 31, 2009 8:06 AMOma Danny Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have found this one NetXtreme Email Verifier Component . It is good for me, hope this helps you.
  • Friday, November 06, 2009 3:39 PMEfran Cobisi [cobisi.com] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi all,

    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.