检查电子邮件地址对System.Net.Mail.MailAddress有效 [英] Check that email address is valid for System.Net.Mail.MailAddress

查看:526
本文介绍了检查电子邮件地址对System.Net.Mail.MailAddress有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,为避免由于无效的电子邮件地址而引发错误,我将执行以下操作:

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@invalid@email.com")
Catch ex As Exception
   'Invalid email
End Try

但是,有一种方法可以验证电子邮件地址对于MailAddress类型的100%有效,而不是依赖于Try..Catch?

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?

我知道那里有很多用于验证电子邮件的正则表达式函数,但是我正在寻找MailAddress类型用来验证其地址的函数.

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.

推荐答案

不幸的是,没有MailAddress.TryParse方法.

您的代码是验证.Net中电子邮件地址的理想方法.

Your code is the ideal way to validate email addresses in .Net.

这篇关于检查电子邮件地址对System.Net.Mail.MailAddress有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆