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 an array.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 Text2Scrape

This Class uses String arrays to store results

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Coderbuddy
{
public class ExtractEmails
{
private string s;
public ExtractEmails(string Text2Scrape)
{
this.s = Text2Scrape;
}



 public string[] Extract_Emails()
{
string[] Email_List = new string[0];
Regex r = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
Match m;
//Searching for the text that matches the above regular expression(which only matches email addresses)
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
//This section here demonstartes Dynamic arrays
if (m.Value.Length > 0)
{
//Resize the array Email_List by incrementing it by 1, to save the next result
Array.Resize(ref Email_List, Email_List.Length + 1);
Email_List[Email_List.Length - 1] = m.Value;
}
}
return Email_List;
}
}
}

The following code snippet is the same as the above one, the only difference is that this method saves the Extracted Email Id’s to a string list instead of an array, I think the following code is more effective and easy to code

This class uses List to store results, and these list are converted into arrays just before they are returned as arrays

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Coderbuddy
{
public class ExtractEmailLists
{
private string s;
public ExtractEmailLists(string Text2Scrape)
{
this.s = Text2Scrape;
}



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

Please comment, if you have any doubts

About these ads

6 Comments on “C# code to extract Email”

  1. [...] page for the search term “C# code to extract email” which was a title of my blog post http://coderbuddy.wordpress.com/2009/10/31/coder-buddyc-code-to-extract-email/, i was even more shocked to see results from pages that has copied this post letter to letter top [...]

  2. lamperd cole says:

    i need fresh emails and that is why i am here.

  3. Rutuparn says:

    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

    • Vamsi says:

      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

  4. fantasitrade says:

    Here’s a video that has a pretty good working example of what you’ve done here:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.