Click here to Skip to main content
13,355,726 members (60,199 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 26 May 2006

E-mail Address Scanner in C#

, 26 May 2006
Rate this:
Please Sign up or sign in to vote.
Scan a website for email addresses and add them to an XML-based file (MSN contact list file)

E-mail Address Scanner


This simple program scans a webpage (within its own basic browser) for email addresses, which you can then view or remove (and even add to) in a list. This list can then be exporte into an MSN Messenger Contacts List file (*.ctt) to add to your MSN contacts.

This article provides source code for you, and explains key points within the code. It does not go over the whole project as a tutorial would.


This was orginally developed as a plugin for the HTMLEditor program, but was the converted to a standalone application for this site.

Key Points in Code

Step 1) Typing in the URL

The firs thing the program must do is allow the user to specify a website to get the addresses from. To do this there is a textbox (textBox1) which accepts a URL. On pressing enter, the previewKeyDown event loads the URL into the web browser, and loads the <BODY> HTML of the page into the rich text box.
This event is as follows:

private void textBox1_PreviewKeyDown ( object sender, PreviewKeyDownEventArgs e )
if (e.KeyCode == Keys.Return)
Status.Text = "Loading file...";
webBrowser1.Navigate ( textBox1.Text );
richTextBox1.Text = webBrowser1.Document.Body.InnerHtml;
Status.Text = "File loaded!";

Next is the real part, the OnClick event of the button labelled "Get email addresses from site / file" is the backbone of this application:

private void btnGet_Click ( object sender, EventArgs e )
int Total = richTextBox1.Text.Length;
int Percent = Total / 100;
Console.WriteLine ( richTextBox1.Text.Length );
Console.WriteLine ( "1% = " + Percent );

To keep the user updated, we get the value for 1% of the total file, so our progress bar can report acurately.
int At;
int Start = 0;
int End = 0;
ProgressBar.Visible = true;
for (int c = 0; c < richTextBox1.Text.Length; c++)
richTextBox1.Select ( c, 1 );
if (richTextBox1.SelectedText == "@")
At = richTextBox1.SelectionStart;
for (int b = At; b >= 0; b--)
richTextBox1.Select ( b, 1 );
if (richTextBox1.SelectedText == " " || richTextBox1.SelectedText == "<" || richTextBox1.SelectedText == "," || richTextBox1.SelectedText == ">")
Start = richTextBox1.SelectionStart + 1;

So, We look for the "@" symbol - obviously because it is a character which MUST be in something to be an email address, then we look for the "<>" brackets to either side of it (b decrements until it finds one to the left, wheras a increments until it finds one to the left)

for (int a = At; a <= richTextBox1.Text.Length; a++)
richTextBox1.Select ( a, 1 );
if (richTextBox1.SelectedText == " " || richTextBox1.SelectedText == "<" || richTextBox1.SelectedText == "," | richTextBox1.SelectedText == ">")
End = richTextBox1.SelectionStart;
richTextBox1.Select ( Start, End - Start );
lb_Emails.Items.Add ( richTextBox1.SelectedText );

here, we select in between the e-mail addresses < > tags in HTML code and add it to our listBox control.

ProgressBar.Value = (c/ Percent);
Status.Text = "Parsing file..." + c / Percent + "%";
Console.WriteLine (c / Percent );
Status.Text = "Success! " + lb_Emails.Items.Count + " items parsed!";

SO, although with the nested loops / ifs it may look complicated, if you break it back down into English the code actually speaks for itself - look for @, find the <> around it and take it out, adding it to the list.

The next stage for the user is to manually prune / add to their list, but the event handlers for these events are so self-explanatory they do not deserve a mention here (no offense).

The final stage is to click "Done" and have your new MSN Contact List file made.

*.CTT files are XML-based, simple documents following this format:

<?xml version="1.0"?>
<service name=".NET Messenger Service">

So, first we need to add the XML version, <messenger>, <service name> and <contactlist> lines, then create a loop adding each <contact> + email + </contact>, and then close those tags, and save it to a CTT file.

This is all done in the button's event handler:

private void btnDone_Click ( object sender, EventArgs e )
SaveFileDialog save = new SaveFileDialog ( );
save.Filter = "Messenger Contacts (*.ctt) | *.ctt";
save.InitialDirectory = "C:\\";
save.FileName = "ContactList";

//Simply initialise a sfd with the option of creating MSN Contact files 

if (save.ShowDialog() == DialogResult.OK)

FileStream fs = new FileStream ( save.FileName, FileMode.Create );
StreamWriter sw = new StreamWriter ( fs );
lb_Emails.Items.Remove ( "..." );
//We use the "..." character to spcify if the user wants to add an entry, so we discount it from out list 

sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine( " <service name=\".NET Messenger Service\">" );
sw.WriteLine ( " <contactlist>" );
foreach (object ob in lb_Emails.Items)
sw.WriteLine ( " <contact>" + ob.ToString ( ) + "</contact>" );
sw.WriteLine ( " </contactlist>" );
sw.WriteLine ( " </service>" );
sw.WriteLine ( "</messenger>" );
sw.Close ( );
fs.Close ( );
//Basic loop to re-create XML structure of *.CTT files, as discussed before. 

Status.Text = "File saved successfully!";

Feel free to experiment with this code / add new features to it :) Have fun


26/05/06: Submitted to CodeProject


I am always willing to accept feedback, positive or otherwise. Feel free to contact me via the following:


Or of course post comments on this site.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

James Gupta
CEO Synap
United Kingdom United Kingdom
Founder & CEO of Synap, an online education platform that uses machine learning to help people learn more in less time.

Software developer, main languages currently are Objective-C, MySQL and Javascript, though I got started on C++, C# and PHP.

You may also be interested in...

Comments and Discussions

GeneralThanks [modified] Pin
Trance Junkie30-May-06 0:08
memberTrance Junkie30-May-06 0:08 
GeneralUnable to find manifest signing certificate in the certificate store... Pin
d00_ape29-May-06 8:03
memberd00_ape29-May-06 8:03 
GeneralRe: Unable to find manifest signing certificate in the certificate store... Pin
James Gupta29-May-06 9:45
memberJames Gupta29-May-06 9:45 
GeneralRe: Unable to find manifest signing certificate in the certificate store... Pin
DaveMon12-Oct-06 10:22
memberDaveMon12-Oct-06 10:22 
GeneralRe: Unable to find manifest signing certificate in the certificate store... Pin
mhusleag5-Feb-07 9:11
membermhusleag5-Feb-07 9:11 
GeneralRe: Unable to find manifest signing certificate in the certificate store... Pin
Dali Hammadi25-Apr-07 1:22
memberDali Hammadi25-Apr-07 1:22 
QuestionEmail Harvester? Pin
Blkbam26-May-06 9:40
memberBlkbam26-May-06 9:40 
AnswerRe: Email Harvester? Pin
James Gupta26-May-06 9:49
memberJames Gupta26-May-06 9:49 
GeneralRe: Email Harvester? Pin
Blkbam26-May-06 13:34
memberBlkbam26-May-06 13:34 
AnswerRe: Email Harvester? Pin
SiR_RuNcibLe_sPooN26-May-06 10:23
memberSiR_RuNcibLe_sPooN26-May-06 10:23 
GeneralRe: Email Harvester? Pin
James Gupta26-May-06 11:04
memberJames Gupta26-May-06 11:04 
JokeRe: Email Harvester? Pin
jmw26-May-06 11:56
memberjmw26-May-06 11:56 
GeneralRe: Email Harvester? Pin
Charlie Williams26-May-06 13:25
memberCharlie Williams26-May-06 13:25 
AnswerRe: Email Harvester? Pin
samiuzzafar7-Dec-06 1:20
membersamiuzzafar7-Dec-06 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 26 May 2006
Article Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid