检查电子邮件地址对于 System.Net.Mail.MailAddress 是否有效

发布于 2024-11-29 11:38:13 字数 531 浏览 1 评论 0 原文

目前,为了避免因电子邮件地址无效而引发错误,我执行以下操作:

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 类型用于验证其地址的函数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烂柯人 2024-12-06 11:38:13

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

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

Unfortunately, there is no MailAddress.TryParse method.

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

旧人九事 2024-12-06 11:38:13

最近,.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

季末如歌 2024-12-06 11:38:13

如果您需要确保给定的电子邮件地址根据 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.

居里长安 2024-12-06 11:38:13

本身并不是这个问题的答案,但为了以防万一有人需要它,我编写了一个 C# 函数,用于使用此方法验证电子邮件地址。

FixEmailAddress("[电子邮件受保护]")

返回<代码>“[电子邮件受保护]"

FixEmailAddress("wa@< a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b776f7e695b63626135787476">[电子邮件受保护],[电子邮件受保护],asdfdsf,[电子邮件受保护]")

返回 "[电子邮件受保护],[email protected]"

我以这种方式处理电子邮件地址列表,因为逗号分隔的电子邮件列表是 MailAddressCollection.Add() 的有效参数

/// <summary>
/// Given a single email address, return the email address if it is valid, or empty string if invalid.
/// or given a comma delimited list of email addresses, return the a comma list of valid email addresses from the original list.
/// </summary>
/// <param name="emailAddess"></param>
/// <returns>Validated email address(es)</returns>  
public static string FixEmailAddress(string emailAddress)
{

   string result = "";

    emailAddress = emailAddress.Replace(";",",");
   if (emailAddress.Contains(","))
   {
       List<string> results = new List<string>();
       string[] emailAddresses = emailAddress.Split(new char[] { ',' });
       foreach (string e in emailAddresses)
       {
           string temp = FixEmailAddress(e);
           if (temp != "")
           {
               results.Add(temp);
           }
       }
       result = string.Join(",", results);
   }
   else
   {

       try
       {
           System.Net.Mail.MailAddress email = new System.Net.Mail.MailAddress(emailAddress);
           result = email.Address;
       }
       catch (Exception)
       {
           result = "";
       }

   }

   return result;

}

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()

/// <summary>
/// Given a single email address, return the email address if it is valid, or empty string if invalid.
/// or given a comma delimited list of email addresses, return the a comma list of valid email addresses from the original list.
/// </summary>
/// <param name="emailAddess"></param>
/// <returns>Validated email address(es)</returns>  
public static string FixEmailAddress(string emailAddress)
{

   string result = "";

    emailAddress = emailAddress.Replace(";",",");
   if (emailAddress.Contains(","))
   {
       List<string> results = new List<string>();
       string[] emailAddresses = emailAddress.Split(new char[] { ',' });
       foreach (string e in emailAddresses)
       {
           string temp = FixEmailAddress(e);
           if (temp != "")
           {
               results.Add(temp);
           }
       }
       result = string.Join(",", results);
   }
   else
   {

       try
       {
           System.Net.Mail.MailAddress email = new System.Net.Mail.MailAddress(emailAddress);
           result = email.Address;
       }
       catch (Exception)
       {
           result = "";
       }

   }

   return result;

}

つ可否回来 2024-12-06 11:38:13

有些字符在某些服务提供商中有效,但在其他服务提供商中则无效! 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 the MailAddress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文