Canarys | IT Services


Reading GMAIL Inbox using ASP.NET


In this blog I will explain how to read emails from pop3 server using the free open source library OpenPop.Net. This library provides you very easy API to access and read emails from the POP3 Servers.
Download the library from the following URL.
Download OpenPop.Net
Once you have the library we can start building our sample application that will access and read emails from the POP3 servers. You will need to add the reference of the OpenPop.DLL to your project using Add Reference option in Visual Studio
HTML Markup


Mail Server Name:

Email ID:




I have also placed an ASP.Net GridView control which will display the emails in tabular format to the users. I have added anasp:HyperLinkField for the Message Subject so that when it is clicked the complete message is displayed to the user.


using OpenPop.Pop3;

using OpenPop.Mime;

using System.Data;

Fetching and Reading list of emails from server

protected void Read_Emails(object sender, EventArgs e)


    Pop3Client pop3Client;

    if (Session[“Pop3Client”] == null)


        pop3Client = new Pop3Client();

        pop3Client.Connect(txtMailServer.Text, int.Parse(txtPort.Text), chkSSL.Checked);

        pop3Client.Authenticate(txtUserName.Text, txtPassword.Text);

        Session[“Pop3Client”] = pop3Client;




        pop3Client = (Pop3Client)Session[“Pop3Client”];


    int count = pop3Client.GetMessageCount();

    DataTable dtMessages = new DataTable();





    int counter = 0;

    for (int i = count; i >=1 ; i–)


        Message message = pop3Client.GetMessage(i);


        dtMessages.Rows[dtMessages.Rows.Count – 1][“MessageNumber”] = i;

                dtMessages.Rows[dtMessages.Rows.Count – 1][“From”] = message.Headers.From.Address;

        dtMessages.Rows[dtMessages.Rows.Count – 1][“Subject”] = message.Headers.Subject;

        dtMessages.Rows[dtMessages.Rows.Count – 1][“DateSent”] = message.Headers.DateSent.ToLocalTime();


        if (counter > 4)





    gvEmails.DataSource = dtMessages;



On the click of the button btnReadEmails we will execute the event Read_Emails which will fetch and read emails from the POP3 server.

In the above code snippet I am using the Pop3Client class OpenPop.Net Library to connect to the POP3 server and fetching the emails. Below are the steps that describes how the library works

1. The client connects to the POP3 server using the server URL and Port Number

2. The client authenticates the user based on username and password.

3. The client fetches the total count of the messages on the server.

4. Based on the count we run a loop and start fetching the emails from the server.

Note: Since fetching emails is a time consuming process I am fetching only latest 5 emails from the server. You can later on remove that condition as per your requirement


In the GridView I made the Subject field of the Email as Hyperlink and which when clicked redirects the user to another page named ShowMessage.aspx which as the named suggests displays the complete email.

HTML Markup

Below is the HTML Markup of the ShowMessage.aspx page







As you will notice the page is quite simple it has three labels to display

1. From Email

2. Subject of the email

3. Body of the email

Fetch and Display the message

From the main page I pass the MessageNumber as parameter via QueryString to the ShowMessage page. Here based on the MessageNumber I am fetching the complete message using the following code snippet.

protected void Page_Load(object sender, EventArgs e)


    if (!IsPostBack)


        Pop3Client pop3Client = (Pop3Client)Session[“Pop3Client”];

        int messageNumber = int.Parse(Request.QueryString[“MessageNumber”]);

        Message message = pop3Client.GetMessage(messageNumber);

        MessagePart messagePart = message.MessagePart.MessageParts[0];

        lblFrom.Text = message.Headers.From.Address;

        lblSubject.Text = message.Headers.Subject;

        lblBody.Text = messagePart.BodyEncoding.GetString(messagePart.Body);





Leave a Reply

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

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.