System.Net.MailMessage允许一些无效的电子邮件地址格式 [英] System.Net.MailMessage allows some invalid email address formats

查看:69
本文介绍了System.Net.MailMessage允许一些无效的电子邮件地址格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人可能已经知道,正确验证电子邮件地址可能有些噩梦。您可以整天搜索与当前RFC标准匹配的C#正则表达式,然后会找到不同的正则表达式,它们给出不同的结果。

As many people may already be aware, correctly validating email addresses can be somewhat of a nightmare. You can search all day long for a C# regex that matches the current RFC standards, and you'll find different regex expressions that give different results.

如果您查看<一个href = http://en.wikipedia.org/wiki/Email_address#Local_part rel = nofollow> http://en.wikipedia.org/wiki/Email_address#Local_part ,您会看到不允许在本地部分的开头或结尾使用句点。也不允许连续两个周期。但是,以下NUnit测试证明System.Net.MailMessage允许您实例化一些无效电子邮件地址格式的MailMessage对象。

If you look at http://en.wikipedia.org/wiki/Email_address#Local_part, you'll see that a period at the beginning or end of the local part is not allowed. Two consecutive periods are also not allowed. However, the following NUnit test proves that System.Net.MailMessage allows you to instantiate a MailMessage object for some invalid email address formats.

[Test]
[TestCase(@"foobar@exampleserver")] //technically valid from the wiki article
[TestCase(@"jsmith@[192.168.2.1]")] //technically valid from the wiki article
[TestCase(@"niceandsimple@example.com")] //vanilla email address
[TestCase(@"very.common@example.com")] //also standard
[TestCase(@"a.little.lengthy.but.fine@dept.example.com")] //long with lots of periods
[TestCase(@"disposable.style.email.with+symbol@example.com")] //disposable with the + symbol
[TestCase(@"other.email-with-dash@example.com")] //period and dash in local part
[TestCase(@"user-test-hyphens@example-domain.com")] //lots of hyphens
[TestCase(@"!#$%&'*+-/=?^_`{|}~@example-domain.com")] //all these symbols are allowed in local part
[TestCase(@"ër_%لdev@gكňil.com")] //characters outside the ascii range are permitted
[TestCase(@"""abcdefghixyz""@example.com")] //technically valid
//[TestCase(@"abc.""defghi"".xyz@example.com")] //technically valid, but .NET throws exception
public void CanCreateMailMessageObjectTest(string emailAddress)
{
     var mailMessage = new System.Net.Mail.MailMessage("noreply@example.com", emailAddress);  
}

以上所有测试用例都通过了,最后一个除外。

All of the above test cases pass except the last one.

[Test]
[TestCase(@".test@example.com")] //leading period
[TestCase(@"test.@example.com")] //period at end of local part <---FAIL
[TestCase(@"test..example@example.com")] //double period in local part <---FAIL
[TestCase(@"foobar@example!#$%^&*()=server.com")] //special characters in domain part
[TestCase(@"Abc.example.com")] //No @ separating local and domain part
[TestCase(@"A@b@c@example.com")] //more than one @ symbol
[TestCase(@"just""not""right@example.com")] //quoted strings must be dot separated
[TestCase(@"a""b(c)d,e:f;g<h>i[j\k]l@example.com")] //special symbols "(),:;<>@[\] not inside quotes
[TestCase(@"[test@example.com")] //leading special symbol in local part
[TestCase(@"this is""not\allowed@example.com")] //spaces not in quotes
[TestCase(@"this\ still\""not\\allowed@example.com")] //backslashes not in quotes
[ExpectedException(typeof (System.FormatException))]
public void CannotCreateMailMessageObjectTest(string emailAddress)
{
    var mailMessage = new System.Net.Mail.MailMessage("noreply@example.com", emailAddress);
}

为什么在地球上进行 test。@ example.com test..example @ example.com 无法引发System.FormatException吗? Microsoft或Wikipedia,谁在这里错了?是否有任何电子邮件地址被保留以允许尾随期或两倍期?我的验证应该允许他们吗?我已经采取了适当的异常处理措施,以使我的电子邮件传递服务在发生异常的情况下能够正常运行,但是我想抛出无效或保证会引发异常的电子邮件地址。

Why on earth do test.@example.com and test..example@example.com fail to throw a System.FormatException? Who is wrong here, Microsoft or Wikipedia? Are there any email addresses that are grandfathered in to allow trailing period or double period? Should my validation allow them? I have the proper exception handling in place to allow my email delivery service to carry on about its day if an exception occurs, but I'd like to throw out email addresses that are either invalid or guaranteed to throw an exception.

推荐答案

没有解释原因,但是 System.Net.Mail.MailAddress 上的MSDN文档指出该地址格式受支持:

There's no explanation of why, but MSDN's docs on System.Net.Mail.MailAddress calls out that this address format is supported:


MailAddress类支持以下邮件地址格式:

The MailAddress class supports the following mail address formats:


  • 用户名中的连续和尾随点。例如,用户...名称.. @ host。

因此,这不是Bug MailAddress 类-明确支持该形式。但是我不知道支持他们的原因是什么。我以为也许某些系统实际上接受了它们,而MS则认为有必要支持这种情况。

So it's not a bug in the MailAddress class - that form is explicitly supported. But I don't know what the reason is for supporting them. I'd assume that maybe some systems actually accept them, and MS feels the need to support that scenario.

另一方面,尽管我了解需要提供我对电子邮件地址进行了一些验证,我个人认为验证不需要太严格。无论如何,系统需要处理不良但在语法上有效的地址。另一方面,在本地部分看来,加倍的句号或句号可能是常见的拼写错误,因此我可以理解为什么您可能希望它们无法通过验证。

On the other hand, while I can understand the need to provide some validation of email addresses, my personal opinion is that there's little need to be super strict in the validation. The system needs to handle bad, but syntactically valid, addresses anyway. On the other other hand, it seems like a doubled period or period at the end of the local-part might be a common typo, so I can understand why you might want them to fail validation.

这篇关于System.Net.MailMessage允许一些无效的电子邮件地址格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆