Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What about this?</p> <pre><code>bool IsValidEmail(string email) { try { var addr = new System.Net.Mail.MailAddress(email); return addr.Address == email; } catch { return false; } } </code></pre> <p>To clarify, the question is asking whether a particular string is a valid representation of an e-mail address, not whether an e-mail address is a valid destination to send a message. For that, the only real way is to send a message to confirm.</p> <p>Note that e-mail addresses are more forgiving than you might first assume. These are all perfectly valid forms:</p> <ul> <li>cog@wheel</li> <li>"cogwheel the orange"@example.com</li> <li>123@$.xyz</li> </ul> <p>For most use cases, a false "invalid" is much worse for your users and future proofing than a false "valid". Here's an <a href="http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/" rel="noreferrer">article that used to be the accepted answer</a> to this question (that answer has since been deleted). It has a lot more detail and some other ideas of how to solve the problem.</p> <p>Providing sanity checks is still a good idea for user experience. Assuming the e-mail address is valid, you could look for known top-level domains, check the domain for an MX record, check for spelling errors from common domain names (gmail.cmo), etc. Then present a warning giving the user a chance to say "yes, my mail server really does allow as an email address."</p> <hr> <p>As for using exception handling for business logic, I agree that is a thing to be avoided. But this is one of those cases where the convenience and clarity may outweigh the dogma.</p> <p>Besides, if you do anything else with the e-mail address, it's probably going to involve turning it to a MailAddress. Even if you don't use this exact function, you will probably want to use the same pattern. You can also check for specific kinds of failure by catching <a href="https://msdn.microsoft.com/en-us/library/591bk9e8(v=vs.110).aspx#Exceptions" rel="noreferrer">different exceptions</a>: null, empty, or invalid format.</p> <hr> <p>Per Stuart's comment, this compares the final address with the original string instead of always returning true. MailAddress tries to parse a string with spaces into "Display Name" and "Address" portions, so the original version was returning false positives.</p> <hr> <p>--- Further reading ---</p> <p><a href="https://msdn.microsoft.com/da-dk/library/system.net.mail.mailaddress.address(v=vs.110).aspx" rel="noreferrer">Documentation for System.Net.Mail.MailAddress</a></p> <p><a href="https://en.wikipedia.org/wiki/Email_address#Local-part" rel="noreferrer">Explanation of what makes up a valid email address</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload