C# code to extract Email


Hi,
In this post, i am going to provide the readers with sample code to Extract Email addresses from a given string and store them into a list.I will give the code as Method, so that you can easily import it into your application.This code can be easily used to extract Email Id’s from a webpage, just pass the HTML of the web page as the textToScrape to the method.

Advertisements

6 thoughts on “C# code to extract Email

  1. Hi Actually i have used you code and passed the url according to you instruction but it return string[0]. and nothing is happened.

    Can you please let me know how to pass the HTML?

    Thanks

    Like

    • The code was written 2 years back, so i was a bit rusty back than

      anyway, i modified the code a bit for you and made this method you can directly add this method to your code, you don’t even need to add any name spaces
      just add this code and call it like Extract_Email(“somehtmltexthere”);

      public string[] Extract_Emails(string htmlText)
      {
      System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@”[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}”, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
      System.Text.RegularExpressions.Match m;
      System.Collections.Generic.List<string> results = new System.Collections.Generic.List<string>();
      for (m = r.Match(htmlText); m.Success; m = m.NextMatch())
      {
      if (!(results.Contains(m.Value)))
      results.Add(m.Value);
      }
      return results.ToArray();
      }

      and one more thing, if you are getting empty array , then may be this html text you are passing doesn’t have any emailId s in it

      Like

Comments are closed.