<< Fetch and Read emails from POP3 mail server using C# and VB.Net – Part I

This is the second part of the article Fetch and Read emails from POP3 mail server using C# and VB.Net - Part I where I explained How to fetch the email list from the mailbox and display the same in Grid using the OpenPop.Net Open Source library.
In the first part, 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.
<form id="form1" runat="server">
    <div>
        From: <asp:Label ID="lblFrom" runat="server" Text="" />
        <br />
        Subject: <asp:Label ID="lblSubject" runat="server" Text="" />
        <br />
        Body: <asp:Label ID="lblBody" runat="server" Text="" />
    </div>
</form>
 
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.
C#
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);
    }
}
 
VB.Net
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
   If Not IsPostBack Then
       Dim pop3Client As Pop3Client = CType(Session("Pop3Client"), Pop3Client)
       Dim messageNumber As Integer = Integer.Parse(Request.QueryString("MessageNumber"))
       Dim message As Message = pop3Client.GetMessage(messageNumber)
       Dim messagePart As MessagePart = message.MessagePart.MessageParts(0)
       lblFrom.Text = message.Headers.From.Address
       lblSubject.Text = message.Headers.Subject
       lblBody.Text = messagePart.BodyEncoding.GetString(messagePart.Body)
   End If
End Sub
 
 
Screenshot
Read and fetch email Subject and Body from pop3 server using c# and vb.net
 
 
Downloads
You can download the complete source code in VB.Net and C# using the link below.

<< Fetch and Read emails from POP3 mail server using C# and VB.Net – Part I