以下电子邮件格式无效
fulya_42_@hotmail.coö
但到目前为止,我在c#上找到并尝试的所有验证都表明这是正确的电子邮件,而不是
如何使用c# 4.5.2验证电子邮件是否有效?谢谢
Ok更新的问题
我问的原因是,当您尝试通过电子邮件发送此地址时,最大的电子邮件服务之一的mandrill会抛出内部服务器错误
因此,他们在发送电子邮件之前一定是在使用某种验证。我的目的是在尝试谢谢之前找出他们是用什么来消除这样的电子邮件
发布于 2015-05-26 10:46:13
通过使用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");
}
发布于 2015-05-26 10:48:26
检查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;
}
}
也可以查看@Cogwheel here C# code to validate email address
发布于 2015-05-26 10:48:43
可以使用以下命令匹配电子邮件地址的正则表达式:
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);
参考MSDN
但在你的案例中:
fulya_42_@hotmail.coö
如果您正在检查电子邮件地址的有效性从".coo
“,这是无效的,根据您的观察,它不会显示任何错误,因为正则表达式没有验证,因此您必须手动添加一些您接受的域名,如: gmail.com,yahoo.com等。
在SonerGonul对问题的评论中正确地说了
谢谢
https://stackoverflow.com/questions/30456217
复制相似问题