Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Net.MailMessage允许某些无效的电子邮件地址格式_C#_.net_Regex_Email Validation_Rfc - Fatal编程技术网

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

C# System.Net.MailMessage允许某些无效的电子邮件地址格式,c#,.net,regex,email-validation,rfc,C#,.net,Regex,Email Validation,Rfc,许多人可能已经意识到,正确验证电子邮件地址可能有点像噩梦。您可以整天搜索与当前RFC标准匹配的C#regex,您会发现不同的regex表达式给出不同的结果 如果查看,您将看到不允许在局部部分的开始或结束处使用句点。也不允许连续两个时段。但是,下面的NUnit测试证明System.Net.MailMessage允许您为一些无效的电子邮件地址格式实例化MailMessage对象 [Test] [TestCase(@"foobar@exampleserver")] //technically vali

许多人可能已经意识到,正确验证电子邮件地址可能有点像噩梦。您可以整天搜索与当前RFC标准匹配的C#regex,您会发现不同的regex表达式给出不同的结果

如果查看,您将看到不允许在局部部分的开始或结束处使用句点。也不允许连续两个时段。但是,下面的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);  
}
除最后一个测试用例外,上述所有测试用例均通过

[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);
}
[测试]
[TestCase@]。test@example.com“”]//前导周期

[TestCase(@“test@example.com”)]//本地部分末尾的句点好吧,既然RFC定义了标准,那么微软的实现就不正确了

如果您想做更好的验证,请尝试回答问题

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

局部零件必须是无引号的:它不支持带引号的局部零件或带引号的标签

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


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

没有解释原因,但要求支持此地址格式:

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

  • 用户名中的连续点和尾随点。例如,user…name..@host
因此,它不是
maildaddress
类中的一个bug-该表单是明确支持的。但我不知道支持他们的理由是什么。我假设有些系统实际上接受了它们,微软觉得有必要支持这种情况


另一方面,虽然我可以理解需要提供一些电子邮件地址验证,但我个人的意见是,在验证过程中几乎不需要过于严格。无论如何,系统需要处理坏的但语法上有效的地址。另一方面,似乎两倍周期或本地部分末尾的周期可能是常见的打字错误,因此我可以理解为什么您可能希望它们无法通过验证。

感谢您发布了3年半的解决方案,但这并不是我真正想要的。我可以从FluentValidation和Microsoft获得正则表达式,也可以自己编写正则表达式,但问题是它们都不同。这篇文章并没有要求使用正则表达式,但质疑为什么MailMessage不会引发FormatException。“可能是一个常见的拼写错误”-->。因为这个原因,我让他们失望了。我通常只想让a)显然无效的地址失败,或者b)我知道无法传递的地址失败,因为MailAddress/MailMessage抛出了一个异常(我可以处理,但仍然…)。