许多人可能已经意识到,正确验证电子邮件地址可能有点像一场噩梦。您可以整天搜索与当前 RFC 标准匹配的 C# 正则表达式,您会发现不同的正则表达式会给出不同的结果。
如果你看http://en.wikipedia.org/wiki/Email_address#Local_part 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(@"[email protected] /cdn-cgi/l/email-protection")] //vanilla email address
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //also standard
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //long with lots of periods
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //disposable with the + symbol
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //period and dash in local part
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //lots of hyphens
[TestCase(@"!#$%&'*+-/=?^_`{|}[email protected] /cdn-cgi/l/email-protection")] //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""[email protected] /cdn-cgi/l/email-protection")] //technically valid, but .NET throws exception
public void CanCreateMailMessageObjectTest(string emailAddress)
{
var mailMessage = new System.Net.Mail.MailMessage("[email protected] /cdn-cgi/l/email-protection", emailAddress);
}
除最后一个之外,上述所有测试用例均通过。
[Test]
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //leading period
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //period at end of local part <---FAIL
[TestCase(@"[email protected] /cdn-cgi/l/email-protection")] //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@[email protected] /cdn-cgi/l/email-protection")] //more than one @ symbol
[TestCase(@"just""not""[email protected] /cdn-cgi/l/email-protection")] //quoted strings must be dot separated
[TestCase(@"a""b(c)d,e:f;g<h>i[j\k][email protected] /cdn-cgi/l/email-protection")] //special symbols "(),:;<>@[\] not inside quotes
[TestCase(@"[[email protected] /cdn-cgi/l/email-protection")] //leading special symbol in local part
[TestCase(@"this is""not\[email protected] /cdn-cgi/l/email-protection")] //spaces not in quotes
[TestCase(@"this\ still\""not\\[email protected] /cdn-cgi/l/email-protection")] //backslashes not in quotes
[ExpectedException(typeof (System.FormatException))]
public void CannotCreateMailMessageObjectTest(string emailAddress)
{
var mailMessage = new System.Net.Mail.MailMessage("[email protected] /cdn-cgi/l/email-protection", emailAddress);
}
到底为什么要这样做[email protected] /cdn-cgi/l/email-protection
and [email protected] /cdn-cgi/l/email-protection
无法抛出 System.FormatException?微软还是维基百科,谁错了?是否有任何电子邮件地址可以允许尾随句点或双句点?我的验证应该允许他们吗?我有适当的异常处理,以便在发生异常时允许我的电子邮件传送服务继续正常运行,但我想丢弃无效或肯定会引发异常的电子邮件地址。
没有解释为什么,但是MSDN 的文档System.Net.Mail.MailAddress http://msdn.microsoft.com/en-us/library/System.Net.Mail.MailAddress.aspx指出支持此地址格式:
MailAddress 类支持以下邮件地址格式:
...
- 用户名中的连续点和尾随点。例如,用户...名称..@主机。
所以这不是一个错误MailAddress
类 - 明确支持该形式。但我不知道支持他们的理由是什么。我认为也许有些系统实际上接受它们,并且微软认为有必要支持这种情况。
另一方面,虽然我可以理解需要对电子邮件地址进行一些验证,但我个人的观点是,在验证方面几乎没有必要非常严格。无论如何,系统需要处理错误但语法有效的地址。另一方面,本地部分末尾的双句点或句点似乎是常见的拼写错误,所以我可以理解为什么您可能希望它们验证失败。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)