System.Net.MailMessage允许一些无效的电子邮件地址格式

jre*_*csu 5 .net c# regex rfc email-validation

许多人可能已经意识到,正确验证电子邮件地址可能有点噩梦.您可以整天搜索与当前RFC标准匹配的C#正则表达式,并且您将找到不同的正则表达式,它们会产生不同的结果.

如果查看http://en.wikipedia.org/wiki/Email_address#Local_part,您将看到不允许在本地部分的开头或结尾处出现句点.也不允许连续两个时期.但是,以下NUnit测试证明System.Net.MailMessage允许您为某些无效的电子邮件地址格式实例化MailMessage对象.

[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);  
}
Run Code Online (Sandbox Code Playgroud)

除最后一个测试用例外,所有上述测试用例都通过.

[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);
}
Run Code Online (Sandbox Code Playgroud)

为什么在地球上做test.@example.com并且test..example@example.com没有抛出System.FormatException?谁在这里错了,微软还是维基百科?是否有任何电子邮件地址被允许进入允许尾随期或双期?我的验证应该允许吗?我有适当的异常处理,以允许我的电子邮件传递服务在发生异常时继续进行当天,但我想丢弃无效或保证抛出异常的电子邮件地址.

Nic*_*rey 0

嗯,既然 RFC 定义了标准,那么 Microsoft 的实现就是不正确的。

如果您想进行更好的验证,请尝试我在问题C# 电子邮件地址验证的答案中发布的验证器

它应该正确(且严格)地验证您可能遇到的任何local-part@domain格式的“正常”电子邮件地址,尽管它不会处理允许的新的非 ASCII 样式内容。我不保证自几年前以来它没有出现一点点腐烂,而且自从我写它以来电子邮件 RFC 已经更新了。

本地部分必须不加引号:它不支持带引号的本地部分或带引号的标签。

就域部分而言,我的验证器不支持 IPv4 或 IPv6 文字(尽管添加它并不困难)。

如果您想允许任何/所有符合 RFC 的地址,事情就会变得更加困难。