The below email is not valid format
fulya_42_@hotmail.coö
But all validations i found and tried so far with c# said it is correct email which is not
How can i validate email is valid or not with c# 4.5.2? thank you
Ok updated question
The reason i am asking is one of the biggest email service mandrill api throws internal server error when you try to email this address
So they must be using some kind of validation before even trying to send out email. My aim is finding what are they using to eliminate such emails before try thank you
By using Regex
string emailID = "fulya_42_@hotmail.coö";
bool isEmail = Regex.IsMatch(emailID, @"\A(?:[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])?)\Z");
if (isEmail)
{
Response.Write("Valid");
}
Check the below class from https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx
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.
try {
strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException) {
return false;
}
if (invalid)
return false;
// Return true if strIn is in valid e-mail format.
try {
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][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException) {
return false;
}
}
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;
}
}
Or take a look at @Cogwheel answer here C# code to validate email address
Regular expression for e-mail address can be matched using below :
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);
Reference MSDN
But in your case :
fulya_42_@hotmail.coö
If you are checking the validity of email address from the " .coo
" which is not valid as per your observation, it will not show any error as the regex doesnt validate that,hence you have to manually add some domains which you accept like : gmail.com , yahoo.com etc.
rightly said in the comments for the question by SonerGonul
Thanks
First off, the email address you provided is in a valid format . -- You can take it a step further and verify if the domain is valid or not as well, if you like; but either way, you should be validating ownership, in which case, you will know that the email address and domain are valid.
Secondly, you should really not be using Regex to validate an email address; email address validation is built-in to the .NET framework, and you shouldn't be recreating the wheel for something like this.
A simple validation function that performs both checks, looks like this:
public static bool IsValidEmailAddress(string emailAddress, bool verifyDomain = true)
{
var result = false;
if (!string.IsNullOrWhiteSpace(emailAddress))
{
try
{
// Check Format (Offline).
var addy = new MailAddress(emailAddress);
if (verifyDomain)
{
// Check that a valid MX record exists (Online).
result = new DnsStubResolver().Resolve<MxRecord>(addy.Host, RecordType.Mx).Any();
}
else
{
result = true;
}
}
catch (SocketException)
{
result = false;
}
catch (FormatException)
{
result = false;
}
}
return result;
}
To run the code, you will need to install the NuGet package ARSoft.Tools.Net
, which is required for MX record lookup*, and you will need to add the appropriate using declarations for the various classes used (should be pretty automatic in VisualStudio these days).
(*: Simply checking for a valid host name via System.Net.Dns.GetHost*
is not enough, as that can give you some false negatives for some domains which only have MX entries, such as admin@fubar.onmicrosoft.com
, etc.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.