简体   繁体   中英

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

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. 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.

Excerpt from 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. Only you need to do a foreach loop. 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

It looks as if the simply approach would work for your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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