Click here to Skip to main content
Click here to Skip to main content

E-mail Address Scanner in C#

By , 26 May 2006
 

E-mail Address Scanner

Introduction

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.

Background

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;
break;
}
}

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;
break;
}
}
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"?>
<messenger>
<service name=".NET Messenger Service">
<contactlist>
<contact>email@address.com</contact>
<contact>email@address2.com</contact>
</contactlist>
</service>
</messenger>

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("<messenger>");
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

History



26/05/06: Submitted to CodeProject

Feedback

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

MSN: jamespraveen@aol.com
E-Mail: james@magclan.cwhnetworks.com
Forum: http://www.just-code-it.net

Or of course post comments on this site.

License

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
Web Developer
United States United States
Member
I live in England, UK in a small town which only just got broadband...
 
In my spare time, I run a small website design, hosting and maintenance business at www.jamesgupta.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks [modified]memberTrance Junkie29 May '06 - 23:08 
Now i know how they spam me everytime, i get emails from people i dont even know, well now its payback time !
Mad | :mad:
 
-- modified at 5:09 Tuesday 30th May, 2006
GeneralUnable to find manifest signing certificate in the certificate store...memberd00_ape29 May '06 - 7:03 
I got:
 
Error 1 Unable to find manifest signing certificate in the certificate store. MSN List
 
when trying to compile the sample project...
 
_____________________________
 
...and justice for all
 
APe
GeneralRe: Unable to find manifest signing certificate in the certificate store...memberJames Gupta29 May '06 - 8:45 
Try making a new project and copying all of the data from that into there (simple copy and paste commands) to see if that helps.
 
James
 
just-code-it.net
 
Managed DirectX & C# Tutorials
GeneralRe: Unable to find manifest signing certificate in the certificate store...memberDaveMon12 Oct '06 - 9:22 
same problem on my machine also
GeneralRe: Unable to find manifest signing certificate in the certificate store...membermhusleag5 Feb '07 - 8:11 
just in case someone else is in a similar the situation the answer is here :
 
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=162851&SiteID=1
 
d

GeneralRe: Unable to find manifest signing certificate in the certificate store...memberDali Hammadi25 Apr '07 - 0:22 
Hi
Go to the "Signing" tab of the project properties and unchecked "Sign the ClickOnce manifests".
I solved this problem by this way.
 

QuestionEmail Harvester?memberBlkbam26 May '06 - 8:40 
Not that I'm bashing your attempt here but what else would this be used for besides to harvest emails for spam? Confused | :confused: And why post such a thing giving more people access to such methods? Unsure | :~ Suspicious | :suss:
AnswerRe: Email Harvester?memberJames Gupta26 May '06 - 8:49 
That is a valid point.
 
Originally I made the program to search my forum (which at the time had over 100 members), more specifically its member list to create a file for MSN which members could use to add each other quickly.
 
This is the intention of the program, that and purely educational use (web browsers, xml etc).
 

If anyone wants to harvest emails for spam, they'd do it with or without this program, but I beleive that the people on this site have far better things to do than that, and are more responsible, based on posts I've seen.
 
James
 
just-code-it.net
 
Managed DirectX & C# Tutorials
GeneralRe: Email Harvester?memberBlkbam26 May '06 - 12:34 
Don't get me wrong, I do see the value of your code. I just thought I'd put the thought out there and see what other people thought. And though I personally don't have a good use for this example, I do like the searching HTML aspect of it and may have a use for it in other ways.
 
I think the spamming the spammers idea just leads to the same end; spam.
 
James Gupta wrote:
If anyone wants to harvest emails for spam, they'd do it with or without this program, but I beleive that the people on this site have far better things to do than that, and are more responsible, based on posts I've seen.

 
I'd love to believe that the people that read this site only read it for good but we have to face facts, people search the internet for examples of code no matter what their intentions are.
AnswerRe: Email Harvester?memberSiR_RuNcibLe_sPooN26 May '06 - 9:23 
I think this could be useful in countering spam.
 
If you receive a spam with an URL use this to mine the spamer's
web site and send that bothersome spam back to the source (all the email
addresses on the URL) and may be a few more then just one to get the point across.Laugh | :laugh:

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

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