About the author

Miron Abramson
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments



The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Grab all email addresses from string, file or site

While I was working on a "Social network" project, I had to give the user interface to 'Invite friends' - as in every social network application. So, I add a page with 10 TextBoxes, add a RegularExpression validator to each one, let the user fill them, and send email to each email he filled. The users complained that it is not comfortable couse most of them were copy their friend's emails from their gmail account, and gmail uses a big TextBox with all the emails and names mixed. So the solution to do the same functionality is quite simple.  Using RegularExpression we can get a MatchCollection of all strings that match our regular expression pattern. I used the following regular expression, but you can use anything. (I'm open for suggestions) :

 private const string strRegex = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";

using System.Text.RegularExpressions; 

public System.Collections.Specialized.StringCollection CollectFromString(string input)
    System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
    if (string.IsNullOrEmpty(input)) return sc;
    MatchCollection mc = Regex.Matches(input, strRegex, RegexOptions.IgnoreCase);
    for (int i = 0; i < mc.Count; i++)
        if (!sc.Contains(mc[i].Value))
    return sc;

With this method you can just "drop" a big multi-line TextBox, and let the user enter his emails with any format he want - emails sperates with ',' or ';' or with ever char that is not valid as en email, and even mix the emails with names by copy & paste from his favorite email client.  The same functionality but collecting from file is quite the same:

public System.Collections.Specialized.StringCollection CollectFromFile(string fileName)
    System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
    if (string.IsNullOrEmpty(fileName)) return sc;
    if (!System.IO.File.Exists(fileName))
        throw new System.IO.FileNotFoundException();
    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
        string str = sr.ReadToEnd();
        return CollectFromString(str);

And the last method  is to collect all emails from a given web site (one page):

public System.Collections.Specialized.StringCollection CollectFromSite(string url)
    System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
    if (string.IsNullOrEmpty(url)) return sc;

    System.IO.Stream st = null;

    if (!url.StartsWith("http://"))
        url = "http://" + url;

        // make a Web request
        System.Net.WebRequest req = System.Net.WebRequest.Create(url);
        // get the response and read from the result stream
        System.Net.WebResponse resp = req.GetResponse();
        st = resp.GetResponseStream();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
            return CollectFromString(sr.ReadToEnd());
        if (st != null)


In the source code bellow, I  added some more functionalities and wrapped all in a collection to be "generic" and it can be use easly in other projects. 

CustomCollections.rar (4.10 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | C#
Posted by Miron on Thursday, September 13, 2007 1:23 AM
Permalink | Comments (1) | Post RSSRSS comment feed