15473

Validaiting emails with Net.Mail MailAddress

I have read on SO about validating emails with MailAddress and have produced the folowing code:

try { var address = new MailAddress(value.Email); } catch (FormatException) { emailError = "Invalid Email"; }

To my great surprise the string ndskfndlfk@sdflsdf validates as good email. Any idea why is it so?

Answer1:

If you really need to filter those email addresses, you could use the little utility found here, which uses Regex to validate email addresses. Note that this is the .Net 4.0 version - it's a little different for 4.5.

using System; using System.Globalization; using System.Text.RegularExpressions; public class RegexUtilities { bool invalid = false; public bool IsValidEmail(string strIn) { invalid = false; if (String.IsNullOrEmpty(strIn)) return false; // Use IdnMapping class to convert Unicode domain names. strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper); if (invalid) return false; // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$", RegexOptions.IgnoreCase); } private string DomainMapper(Match match) { // IdnMapping class with default property values. IdnMapping idn = new IdnMapping(); string domainName = match.Groups[2].Value; try { domainName = idn.GetAscii(domainName); } catch (ArgumentException) { invalid = true; } return match.Groups[1].Value + domainName; } }

Otherwise, if it's for user registration, I usually just depend on email verification - i.e. the system sends the user an email with a link, and clicking the link therefore validates that the email address is real.

It is possible for a valid email address not to be a real one. In other words, it may even pass a perfect validation system, but that's no guarantee that it exists.

Answer2:

That might not be valid WWW domain name but on a private network I think it is a valid domain name. If you want to get more selective then Regex.

I try and parse legit email (eliminate phony used by spam) using Regex and it is messy.

If you have access to the actual email header then it does not really get easier but it get more accurate.

Recommend

  • Do I really have to give an initial value to all my variables? [duplicate]
  • .NET Regex parsing of the newline character
  • Check if a number in a string contains a valid year in c#
  • How to validate a phone number
  • TypeScript: how to NOT use relative paths to import classes?
  • How to call function from where-object
  • C# Converting MMddyy formated string to Datetime
  • IOS Pushnotification in Arabic language
  • Localization using resources file not working
  • ASP.NET 5: Error with Nuget package in Class library Package
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • UML diagram generator in Visual Studio 2010
  • Save website uploads in a subdomain
  • 3.0.0.M1: SSL - Invalid keystore format
  • Msvcr71.dll Msvcp71.dll missing
  • Change device language on Android 6.0 (Android M)
  • How to log in to an HTTPS website with Jsoup?
  • how does System.Web.HttpRequest::PathInfo work?
  • Swing - Get new component under mouseReleased
  • How to clear text inside text field when radio button is select
  • Launch Runnable Jar from Web Start
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Get object from AWS S3 as a stream
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • trying to dynamically update Highchart column chart but series undefined
  • JTable with a ScrollPane misbehaving
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?