问题描述
目前,为了避免由于无效的电子邮件地址而引发错误,我执行以下操作:
将邮件地址变暗为邮件地址尝试mailAddress = New MailAddress("testing@[email protected]")Catch ex 作为例外'不合规电邮结束尝试
但是,有没有一种方法可以验证电子邮件地址对于 MailAddress
类型是否 100% 有效,而不是依赖于 Try..Catch
?>
我知道有很多用于验证电子邮件的正则表达式函数,但我正在寻找 MailAddress
类型用来验证其地址的函数.
很遗憾,没有 MailAddress.TryParse
方法.
您的代码是在 .Net 中验证电子邮件地址的理想方式.
Currently, to avoid errors from being thrown up due to invalid email addresses, I do the following:
Dim mailAddress As MailAddress
Try
mailAddress = New MailAddress("testing@[email protected]")
Catch ex As Exception
'Invalid email
End Try
However, rather than depending on Try..Catch
, is there a way of validating that the email address will be 100% valid for the MailAddress
type?
I know there a plenty of regex functions out there for validating emails, but I'm looking for the function which the MailAddress
type uses to validate its addresses.
Unfortunately, there is no MailAddress.TryParse
method.
Your code is the ideal way to validate email addresses in .Net.
这篇关于检查电子邮件地址对于 System.Net.Mail.MailAddress 是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!