简体   繁体   English

从文本文件中读取有效的电子邮件地

[英]Reading valid email address from text files

I have a plain text file. 我有一个纯文本文件。 The requirement is to read valid email addresses from text file. 要求是从文本文件中读取有效的电子邮件地址。

The text file does not contain any special characters and contains one word per line. 文本文件不包含任何特殊字符,每行包含一个单词。

Sample 样品

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

I have tried to read the text file as follows, 我试过读取文本文件如下,

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

But unable to find how to extract valid emails from text file. 但无法找到如何从文本文件中提取有效的电子邮件。

I'm using C# 4.0 我正在使用C#4.0

If only your e-mail lines has @ character, you can use 如果只有你的电子邮件行有@字符,你可以使用

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

Ok, I admit it. 好的,我承认了。 This is the worst e-mail validation I have ever seen :) Let's go more deep. 这是我见过的最糟糕的电子邮件验证:)让我们更深入。 You can check your line with using MailAddress class. 您可以使用MailAddress类检查您的行。 Let's define a method for checking e-mail address is valid or not like; 让我们定义一种检查电子邮件地址是否有效的方法;

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

Then we can use; 然后我们可以使用;

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

You can use regular expression to do this. 您可以使用正则表达式执行此操作。 Look into this MSDN example as your reference. 查看此MSDN示例作为参考。

Excerpt from MSDN: 摘自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;
       }
   }

Then use it by: 然后使用它:

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

Hi use regular expression to filter the valid email addresses. 您好使用正则表达式来过滤有效的电子邮件地址。

sample code is given below. 示例代码如下。

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);
        }
    }

you are doing right. 你说得对。 you are calling ReadAllLines method, that returns the array already. 您正在调用ReadAllLines方法,该方法已经返回array Only you need to do a foreach loop. 只有你需要做一个foreach循环。 as: 如:

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

Click here for better understanding. 点击此处以便更好地理解。

It depends on what you mean by valid. 这取决于你的意思是有效的。 Some people take a simple approach and just look for an '@' and at least one '.' 有些人采取简单的方法,只是寻找一个'@'和至少一个'。' in the string. 在字符串中。 Others take email validation much further and attempt to validate addresses against RFC 822 其他人进一步进行电子邮件验证,并尝试根据RFC 822验证地址

It looks as if the simply approach would work for your needs. 看起来简单的方法可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM