【问题标题】:Reading valid email address from text files从文本文件中读取有效的电子邮件地址
【发布时间】:2013-06-18 11:13:56
【问题描述】:

我有一个纯文本文件。要求是从文本文件中读取有效的电子邮件地址。

文本文件不包含任何特殊字符,每行包含一个单词。

示例

test1
test@yahoo.com
test2
test@gmail.com

我尝试如下读取文本文件,

var emails = File.ReadAllLines(@"foo.txt");

但无法找到如何从文本文件中提取有效电子邮件。

我使用的是 C# 4.0

【问题讨论】:

标签: c# .net


【解决方案1】:

如果您的电子邮件行只有@ 字符,您可以使用

var emails = File.ReadAllLines(@"foo.txt").Where(line => line.Contains("@"));

好吧,我承认。这是我见过的最糟糕的电子邮件验证:) 让我们更深入。您可以使用MailAddress 类检查您的线路。让我们定义一个检查电子邮件地址是否有效的方法;

public bool IsValidMailAddress(string s)
{
    try
    {
        MailAddress m = new MailAddress(s);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

那么我们就可以使用了;

var emails = File.ReadAllLines(@"foo.txt").Where(line => IsValidMailAddress(line));

【讨论】:

  • @aiapag 这就是为什么我说only your e-mail lines has @
  • 你还漏掉了最后一个括号。
  • 对我的口味来说太过分了。我不会仅仅为了从中获取一个函数而添加对命名空间的引用,而不管我的应用程序到底做了什么。我建议仅当您需要使用 SMTP 发送电子邮件时才使用 System.Net.Mail 命名空间。 MSDN
【解决方案2】:

您好使用正则表达式过滤有效的电子邮件地址。

示例代码如下。

var emails = File.ReadAllLines(@"foo.txt")
                       .Where(x => x.IsValidEmailAddress());

public static class extensionMethods
    {
        public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }
    }

【讨论】:

    【解决方案3】:

    你做得对。您正在调用ReadAllLines 方法,该方法已经返回array。只有你需要做一个 foreach 循环。如:

    var emails = File.ReadAllLines(@"foo.txt");
    foreach (var email in emails)
    {
        //write validation logic of emails here
    }
    

    Click here 以便更好地理解。

    【讨论】:

      【解决方案4】:

      您可以使用正则表达式来执行此操作。看看这个MSDN example 作为你的参考。

      摘自 MSDN:

         public bool IsValidEmail(string strIn)
         {
             invalid = false;
             if (String.IsNullOrEmpty(strIn))
                return false;
      
             // Use IdnMapping class to convert Unicode domain names. 
             try {
                strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
                                      RegexOptions.None, TimeSpan.FromMilliseconds(200));
             }
             catch (RegexMatchTimeoutException) {
               return false;
             }
      
             if (invalid) 
                return false;
      
             // Return true if strIn is in valid e-mail format. 
             try {
                return Regex.IsMatch(strIn, 
                      @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + 
                      @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$", 
                      RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
             }  
             catch (RegexMatchTimeoutException) {
                return false;
             }
         }
      

      然后使用它:

       var emails = File.ReadAllLines(@"foo.txt");
       foreach(var line in emails)
       {
           if(IsValidEmail(line))
           { //do something with the valid email
           }
       }
      

      【讨论】:

        【解决方案5】:

        这取决于您所说的有效。有些人采取一种简单的方法,只寻找一个“@”和至少一个“.”。在字符串中。其他人则更进一步地进行电子邮件验证,并尝试根据RFC 822 验证地址

        看起来简单的方法可以满足您的需求。

        【讨论】:

          猜你喜欢
          • 2014-03-23
          • 1970-01-01
          • 2016-04-12
          • 1970-01-01
          • 2011-05-15
          • 2016-01-14
          • 1970-01-01
          • 2015-12-28
          相关资源
          最近更新 更多