检查电子邮件地址对于 System.Net.Mail.MailAddress 是否有效
目前,为了避免因电子邮件地址无效而引发错误,我执行以下操作:
Dim mailAddress As MailAddress
Try
mailAddress = New MailAddress("testing@[email protected]")
Catch ex As Exception
'Invalid email
End Try
但是,是否有一种方法可以验证电子邮件地址是否为 Try..Catch
,而不是依赖于 Try..Catch
对于 MailAddress
类型 100% 有效?
我知道有很多用于验证电子邮件的正则表达式函数,但我正在寻找 MailAddress
类型用于验证其地址的函数。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不幸的是,没有
MailAddress.TryParse
方法。您的代码是在 .Net 中验证电子邮件地址的理想方法。
Unfortunately, there is no
MailAddress.TryParse
method.Your code is the ideal way to validate email addresses in .Net.
最近,.NET API 通过 MailAddress.TryCreate 方法进行了扩展,可能会在未来版本中出现,这将消除对常见 try-catch 样板文件的需要:
https://github.com/dotnet/runtime/commit/aea45f4e75d1cdbbfc60daae782d1cfeb700be02
Recently the .NET API was extended with a MailAddress.TryCreate method, probably coming in future releases, which will eliminate the need for the common try-catch boilerplate:
https://github.com/dotnet/runtime/commit/aea45f4e75d1cdbbfc60daae782d1cfeb700be02
如果您需要确保给定的电子邮件地址根据 IETF 标准有效 - 在撰写本文时,
MailAddress
类似乎仅部分遵循该标准 - 我建议您看一下EmailVerify.NET,一个可以轻松集成到您的解决方案中的 .NET 组件。它不依赖正则表达式来执行其工作,但它依赖于内部有限状态机,因此它非常快。免责声明:我是该组件的首席开发人员。
If you need to make sure a given email address is valid according to the IETF standards - which the
MailAddress
class seems to follow only partially, at the time of this writing - I suggest you to take a look at EmailVerify.NET, a .NET component you can easily integrate in your solutions. It does not depend on regular expressions to perform its job but it relies on an internal finite state machine, so it is very very fast.Disclaimer: I am the lead developer of this component.
本身并不是这个问题的答案,但为了以防万一有人需要它,我编写了一个 C# 函数,用于使用此方法验证电子邮件地址。
FixEmailAddress("[电子邮件受保护]")
返回<代码>“[电子邮件受保护]"
FixEmailAddress("wa@< a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b776f7e695b63626135787476">[电子邮件受保护],[电子邮件受保护],asdfdsf,[电子邮件受保护]")
返回
"[电子邮件受保护],[email protected]"
我以这种方式处理电子邮件地址列表,因为逗号分隔的电子邮件列表是 MailAddressCollection.Add() 的有效参数
}
Not really an answer to this question per se, but in case anyone needs it, I wrote a C# function for validating email addresses using this method.
FixEmailAddress("[email protected]")
returns
"[email protected]"
FixEmailAddress("wa@[email protected],[email protected],asdfdsf,[email protected]")
returns
"[email protected],[email protected]"
I process lists of email addresses this way because a comma separated list of emails is a valid parameter for MailAddressCollection.Add()
}
MS 还提供了基于正则表达式的电子邮件验证器的代码:
https://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
MS also provide the code for a regex based email validator:
https://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
有些字符在某些服务提供商中有效,但在其他服务提供商中则无效!
SmtpClient
对服务提供商一无所知。所以它必须尽可能少地过滤。 维基百科充分提到了有关看台的内容。MailAddress 的验证>MSDN。因此,我认为您可以在初始化
MailAddress
之前检查这些验证。Some characters are valid in some service providers but the same is not in others! The
SmtpClient
don't know anything about the service providers. So it has to filter as least as possible. The Wikipedia is welly mentioned about the standers.Validation of
MailAddress
is mentioned on the MSDN. Hence I think you can check for those validations before initializing theMailAddress
.