Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Email - Using VB.NET to Retrieve List of Emails via POP3
Using VB.NET to Retrieve List of Emails via POP3


ASP.NET Email Tutorial

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" />

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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 />"

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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 />"

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks