How to check if email is valid format or not

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


通过使用正则表达式

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

链接地址: http://www.djcxy.com/p/92848.html

上一篇: EmailAttribute在ASP.NET Core的ViewModel中未正确验证

下一篇: 如何检查电子邮件是否为有效格式