许多人可能已经意识到,正确验证电子邮件地址可能有点像噩梦。您可以整天搜索符合当前 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);
}
除了最后一个,以上测试用例全部通过。
[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?谁错了,微软还是维基百科?是否有任何允许尾随句点或双句点的电子邮件地址?我的验证应该允许他们吗?我有适当的异常处理,以允许我的电子邮件传送服务在发生异常时继续运行,但我想丢弃无效或肯定会引发异常的电子邮件地址。
最佳答案
没有解释原因,但是MSDN's docs on System.Net.Mail.MailAddress
指出支持这种地址格式:
The MailAddress class supports the following mail address formats:
...
- Consecutive and trailing dots in user names. For example, user...name..@host.
所以这不是 MailAddress
类中的错误 - 明确支持该表单。但我不知道支持他们的理由是什么。我假设也许某些系统实际上接受了它们,并且 MS 认为有必要支持这种情况。
另一方面,虽然我能理解提供一些电子邮件地址验证的必要性,但我个人认为在验证方面没有必要特别严格。无论如何,系统需要处理错误但语法有效的地址。另一方面,似乎双句号或本地部分末尾的句号可能是常见的拼写错误,因此我可以理解您为什么希望它们无法通过验证。
关于c# - System.Net.MailMessage 允许一些无效的电子邮件地址格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938981/