Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Using VB.NET to Retrieve List of Emails via POP3 (0 Comments)
Admin: Posted Date: April 4, 2010

This tutorial will show you how to use ASP.NET and VB.NET to download a list of emails using a POP3 server.

Using VB.NET to Retrieve List of Emails via POP3

This tutorial will show you how to use ASP.NET and VB.NET to download a list of emails using a POP3 server.

In this tutorial, we will give you an introduction into how to use ASP.NET to login to a POP3 server and check your email. We will be using the System.Net.Sockets namespace to utilize the TcpClient and NetworkStream classes for this example.

The first thing we want to do is create our ASPX page. We will keep it simple and add TextBoxes, a button and a Literal control to the page:

 

<table><tr><td>
POP3 Server: <asp:TextBox ID="fld_Server" runat="server" /></td><td>Port: <asp:TextBox ID="fld_Port" runat="server" Text="110" Columns="3" /></td></tr>
<tr><td>Username: <asp:TextBox ID="fld_Username" runat="server" /></td><td>Password: <asp:TextBox ID="fld_Password" runat="server" TextMode="Password" />
</td></tr></table>
<asp:Button ID="btn_Connect" runat="server" Text="Connect" /><br />
<asp:Literal ID="lit_Status" runat="server" />

We are going to use the button to initiate the connection to the POP3 server. We give the user the ability to input their own server, and login credentials. We should now add the following handler in the codebehind, along with the assembly reference:


 

 

Protected Sub btn_Connect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Connect.Click

End Sub

We can use this handler for all of our code, seeing as we are just connecting to the server and checking messages for this example. In a real-world application, you may want to create a new class for the POP3 functionality.
We start by first create a new instance of the TcpClient, and attempt to connect to the POP3 server. We also instantiate a NetworkStream object, then we output the server response to our Literal control:

 

Dim tcpClient As New TcpClient()
tcpClient.Connect(fld_Server.Text, Convert.ToInt32(fld_Port.Text))

Dim netStream As NetworkStream = tcpClient.GetStream()
Dim strReader As New System.IO.StreamReader(netStream)

lit_Status.Text = strReader.ReadLine() & "<br />"

The next thing we will do is send our Username and Password to the server, and output the Server Response:

 

Dim WriteBuffer(1023) As Byte
Dim enc As ASCIIEncoding = New System.Text.ASCIIEncoding()

WriteBuffer = enc.GetBytes("USER " & fld_Username.Text & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"

WriteBuffer = enc.GetBytes("PASS " & fld_Password.Text & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"

If the Username and Password are correct and verified by the Server, we will get a response starting +OK.
Now, to retrieve a list of messages from the server, we first request using the LIST command, and then we will want to loop through each message to output:

 

WriteBuffer = enc.GetBytes("LIST" & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)

Dim ListMessage As String
Do
ListMessage = strReader.ReadLine()
If ListMessage = "." Then
Exit Do
Else
lit_Status.Text += ListMessage & "<br />"
Continue Do
End If
Loop

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Finally, to close the connection to the server, we send the command QUIT, and again, output the server response:


 

WriteBuffer = enc.GetBytes("QUIT" & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"

The full code-behind for this examples looks something like this:

 

Imports System.Net.Sockets

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btn_Connect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Connect.Click
Dim tcpClient As New TcpClient()
tcpClient.Connect(fld_Server.Text, Convert.ToInt32(fld_Port.Text))

Dim netStream As NetworkStream = tcpClient.GetStream()
Dim strReader As New System.IO.StreamReader(netStream)

lit_Status.Text = strReader.ReadLine() & "<br />"

Dim WriteBuffer(1023) As Byte
Dim enc As ASCIIEncoding = New System.Text.ASCIIEncoding()

WriteBuffer = enc.GetBytes("USER " & fld_Username.Text & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"

WriteBuffer = enc.GetBytes("PASS " & fld_Password.Text & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"

WriteBuffer = enc.GetBytes("LIST" & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)

Dim ListMessage As String
Do
ListMessage = strReader.ReadLine()
If ListMessage = "." Then
Exit Do
Else
lit_Status.Text += ListMessage & "<br />"
Continue Do
End If
Loop

WriteBuffer = enc.GetBytes("QUIT" & Constants.vbCrLf)
netStream.Write(WriteBuffer, 0, WriteBuffer.Length)
lit_Status.Text += strReader.ReadLine() & "<br />"
End Sub
End Class

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: