验证字符串是否是有效的电子邮件地址的最优雅的代码是什么?
当前回答
来自@齿轮的投票最多的答案是最好的答案,但我已经尝试实现trim()字符串方法,因此它将从字符串开始到结束修剪所有用户空白。检查代码下面的完整示例-
bool IsValidEmail(string email)
{
try
{
email = email.Trim();
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
其他回答
我发现这个正则表达式在检查@标记以外的东西和接受奇怪的边缘情况之间是一个很好的权衡:
^[^@\s]+@[^@\s]+(\.[^@\s]+)+$
它至少会让你在@标记周围放一些东西,并且至少放一个正常的域。
There are a lot of strong answers here. However, I recommend that we take a step back. @Cogwheel answers the question https://stackoverflow.com/a/1374644/388267. Nevertheless, it could be costly in a bulk validation scenario, if many of the email address being validated are invalid. I suggest that we employ a bit of logic before we enter into his try-catch block. I know that the following code could be written using RegEx but that could be costly for new developers to understand. This is my twopence worth:
public static bool IsEmail(this string input)
{
if (string.IsNullOrWhiteSpace(input)) return false;
// MUST CONTAIN ONE AND ONLY ONE @
var atCount = input.Count(c => c == '@');
if (atCount != 1) return false;
// MUST CONTAIN PERIOD
if (!input.Contains(".")) return false;
// @ MUST OCCUR BEFORE LAST PERIOD
var indexOfAt = input.IndexOf("@", StringComparison.Ordinal);
var lastIndexOfPeriod = input.LastIndexOf(".", StringComparison.Ordinal);
var atBeforeLastPeriod = lastIndexOfPeriod > indexOfAt;
if (!atBeforeLastPeriod) return false;
// CODE FROM COGWHEEL'S ANSWER: https://stackoverflow.com/a/1374644/388267
try
{
var addr = new System.Net.Mail.MailAddress(input);
return addr.Address == input;
}
catch
{
return false;
}
}
如果你真的,我是说真的想知道一个电子邮件地址是否有效……请邮件交换器来证明它,不需要正则表达式。如果需要,我可以提供代码。
一般步骤如下: 1. 电子邮件地址有域名部分吗?(@ > 0的索引) 2. 使用DNS查询询问域是否有邮件交换器 3.打开到邮件交换器的TCP连接 4. 使用SMTP协议,以电子邮件地址作为接收者打开到服务器的消息 5. 解析服务器的响应。 6. 如果你能走到这一步,就别发短信了,一切都好。
正如您可以想象的那样,这是非常昂贵的时间,并且依赖于smtp,但它确实有效。
这是一个老问题,但我在SO上找到的所有答案,包括最近的答案,都与这个问题类似。然而,在。net 4.5 / MVC 4中,你可以通过从System.ComponentModel中添加[EmailAddress]注释来向表单添加电子邮件地址验证。DataAnnotations,所以我想知道为什么我不能只使用。net的内置功能。
这似乎是可行的,而且在我看来是相当优雅的:
using System.ComponentModel.DataAnnotations;
class ValidateSomeEmails
{
static void Main(string[] args)
{
var email = new EmailAddressAttribute();
email.IsValid("someone@somewhere.com"); //true
email.IsValid("someone@somewhere.co.uk"); //true
email.IsValid("someone+tag@somewhere.net"); //true
email.IsValid("futureTLD@somewhere.fooo"); //true
email.IsValid("fdsa"); //false
email.IsValid("fdsa@"); //false
email.IsValid("fdsa@fdsa"); //false
email.IsValid("fdsa@fdsa."); //false
//one-liner
if (new EmailAddressAttribute().IsValid("someone@somewhere.com"))
return true;
}
}
基于@Cogwheel的回答,我想分享一个修改后的解决方案,适用于SSIS和“脚本组件”:
Place the "Script Component" into your Data Flow connect and then open it. In the section "Input Columns" set the field that contains the E-Mail Adresses to "ReadWrite" (in the example 'fieldName'). Switch back to the section "Script" and click on "Edit Script". Then you need to wait after the code opens. Place this code in the right method: public override void Input0_ProcessInputRow(Input0Buffer Row) { string email = Row.fieldName; try { System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email); Row.fieldName= addr.Address.ToString(); } catch { Row.fieldName = "WRONGADDRESS"; } }
然后,您可以使用条件分割过滤掉所有无效记录或任何您想做的事情。