简体   繁体   English

为什么MailAddress不拒绝电子邮件地址someName @ gmail?

[英]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@hotmailmyname@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.

相关问题 为什么 MailAddress 认为“john@gmail”。 是一个有效的电子邮件地址吗? - Why does MailAddress think 'john@gmail.' is a valid email address? 是“ a @ bbb..com”有效的电子邮件地址吗?,根据MailAddress类,它是 - Is it “a@bbb..com” a valid email address?, According to MailAddress class it is System.Net.Mail.MailAddress不允许Gmail GROUP联系人 - System.Net.Mail.MailAddress does not allow Gmail GROUP contact 将 C# 中的 email 地址字符串解析为 MailAddress object - Parse an email address string in C# to MailAddress object MailAddress 构造函数中的多个地址 - Multiple address in MailAddress constructor 包含无效电子邮件的MailAddress - MailAddress with invalid email 为什么 System.Net.Mail.MailAddress 构造函数在域部分解析带有斜杠“/”的电子邮件? - Why System.Net.Mail.MailAddress constructor parses email with slash "/" in domain part? MailAddress构造函数如何验证邮件地址 - How does the MailAddress constructor validate mail addresses @ html.Listbox(“ someName”,Model.Items)有效,但@ Html.EditorFor(model => model.items)不起作用为什么? - @html.Listbox(“someName”,Model.Items) works but @Html.EditorFor(model=>model.items) does not work Why? 如何使用 gmail smtp 详细信息将邮件发送到指定的电子邮件地址 - How to send mail to specified email address using gmail smtp details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM