[英]Why does MailAddress not reject the email address someName@gmail?
I'm using MailAddress
to attempt to validate a given email. 我正在使用MailAddress
尝试验证给定的电子邮件。
I get a valid email when I give it a string like "someName@gmail". 当我给它一个类似“ someName @ gmail”的字符串时,我会收到一个有效的电子邮件。
I was expecting it to fail with a FormatException
since the email is missing .com at the end. 我期望它以FormatException
失败,因为电子邮件末尾缺少.com。 Why is it not failing to parse the email here? 为什么在这里不能不解析电子邮件?
public bool IsEmailValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
x@host
is a legally formatted email address. x@host
是合法格式的电子邮件地址。 The MailAddress
class only looks at the format of the string, not the validity of the address. MailAddress
类仅查看字符串的格式,而不查看地址的有效性。 You'll have to resolve the host name, send an email to the recipient and wait for the absence of a bounce mail or the presence of a return receipt to fully validate the address. 您必须解析主机名,向收件人发送电子邮件,然后等待缺少退回邮件或回执的情况下,才能完全验证地址。
someName@gmail
is actually a perfectly valid email address, in that it conforms to the email format specification (formally IETF RFC2822 ). someName@gmail
实际上是一个完全有效的电子邮件地址,因为它符合电子邮件格式规范 (正式为IETF RFC2822 )。 However it is almost certainly not routable unless you happen to have a mail server on your local network with the DNS name gmail
. 但是,除非您碰巧在本地网络上有一个DNS名称为gmail
的邮件服务器,否则它几乎是无法路由的。
If you would like to check for this particular problem, I would suggest adding a second check that there is a dot after the @
symbol. 如果您要检查此特定问题,我建议添加第二个检查,以确保@
符号后面有一个点。
EDIT: Please note that this is a business decision that you have to make. 编辑:请注意,这是您必须做出的商业决定。 By invalidating email addresses with a .
通过使电子邮件地址无效.
after the @
, you are breaking the standard email specification, however I feel that in perhaps 99.99% of cases dealing with external mail this is an acceptable tradeoff to make to prevent users mistakenly typing myname@hotmail
or myname@gmail
(I'd wager a common mistake among new Internet users). @
,您违反了标准的电子邮件规范,但是我觉得在处理外部邮件的情况下,可能有99.99%的情况是可以接受的折衷方案,以防止用户错误地键入myname@hotmail
或myname@gmail
(我想下注新互联网用户中的常见错误)。
This could conceivably block legitimate external email, say if I owned the TLD tt
I could have a legitimate email address bob@tt
, and mail handling systems like yours should handle my address without complaining. 可以想象这会阻止合法的外部电子邮件,例如,如果我拥有TLD tt
我可以拥有一个合法的电子邮件地址bob@tt
,并且像您这样的邮件处理系统应处理我的地址而不会抱怨。 However as it has been noted in this answer , this is very uncommon. 但是,正如在此答案中指出的那样,这非常罕见。
Having said that, here's some code that would achieve this: 话虽如此,下面是一些可以实现此目的的代码:
public bool IsEmailValid(string emailaddress){
if (emailaddress == null || !emailaddress.Contains("@") || !emailaddress.Split("@")[1].Contains("."))
{
return false; // Doesn't contain "@" or doesn't contain a "." after the "@"
}
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
Unless you have a very limited range of possible email addresses, don't go trying to limit top level domains. 除非您的可能的电子邮件地址范围非常有限,否则请勿尝试限制顶级域。 For example only allowing .com
, .net
and .org
would be a big mistake - as of writing there are over 1500 TLDs which an email address could conceivably end in 例如,仅允许.com
, .net
和.org
会是一个很大的错误-截至撰写时,有1500多个TLD可以想象电子邮件地址以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.