C# how to fetch all emails on a web page

buraknalpara.com

In this post I will show you how to get all the emails on a web page and display those email addresses on a list box using C#. You can use this simple application or create your own application to create your mail list for your businesses or personal usage.

How to fetch all emails on a web page?

In this simple application we are going to connect to a website and download its all data. After that we are going to try matching email addresses using Regex. Then we will display those emails on a listbox. As you can see it will be a very small application doing great job.

Email Address fetcher design ?

We will have a simple design which contains 2 labels, 1 textbox, 1 listbox and 1 button.

c# how to fetch emails
Fetch email addresses

Basically, we should write the url that we want to fetch email addresses and then we should click Get Mail Addresses button. Then if that web page contains email addresses inside the page our list box should display all the email addresses.

Writing our code

This application will only contain few lines of codes. Below is the code for our application.

private void btnGetMails_Click(object sender, EventArgs e)
        {
            string address = txtUrl.Text;
            WebClient webClient = new WebClient();
            byte[] Data = webClient.DownloadData(address);
            string stringData = Encoding.ASCII.GetString(Data);
            Regex regex = new Regex("([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})");
            MatchCollection collection = regex.Matches(stringData);

            foreach(var email in collection)
            {
                mailListBox.Items.Add(email.ToString());
            }

            lblListResult.Text = "Emails on the website have been listed...";
        }
Try our application

Let’s try our application using on http://unicode.org/consortium/distlist.html#members_email. This page contains 5 emails on its page and our application should be able to fetch them. Run the application and write the url that we want to check then click the Get Mail Addresses button.

You should see something like below after performing the above commands. This shows that we have fetched all emails on a web page.

displaying email addresses on a website
List of emails

HBN

4.9 (97.14%) 7 votes

About the Author: HBN

--Senior QA Engineer-- --Intermediate Web Developer-- --Junior Android Developer--

Leave a Reply

Your email address will not be published. Required fields are marked *