Home and Learn: Intermediate Programming
Receive Email Walkthrough: for C# and VB NET Students
In the previous lesson, we got a list of emails waiting on the pop3 server. In this lesson, we'll download the message itself.
To get the actual message itself, we need to use our ListBox. When an item is clicked in the ListBox, we'll call a Subroutine or method to get the email selected. We'll put all of it in the text box at the bottom of the form.
ListBoxes have a SelectedIndexChanged event that is quite handy for us. The Index number of a ListBox is the item's position in the list. The count starts at zero, though. So item 1 on the Email List above is Index 0 and item 2 is Index 1.
Create a code stub for the SelectedIndexChanged event of your ListBox. (You should know how to do this by now. If not, click the lightning bolt in the Properties area.) Add the following code in VB Net:
Dim indexNumber As String
Dim strRetrieve As String
txtEmail.Text = ""
Try
indexNumber = ( ListBox1.SelectedIndex + 1 ).ToString()
strRetrieve = "RETR " + indexNumber + vbCrLf
GetEmail(strRetrieve)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
And here is the C# code:
string indexNumber;
string strRetrieve;
txtEmail.Text = "";
try
{
indexNumber = (listBox1.SelectedIndex + 1).ToString();
strRetrieve = "RETR " + indexNumber + "\r\n";
GetEmail(strRetrieve);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
First, we set up two string variables, and then clear the Email text box. Inside of a Try Catch block, we get the index number of the item on the list that was selected:
indexNumber = ( ListBox1.SelectedIndex + 1 ).ToString()
We have to add 1 to the SelectedIndex because, as was mentioned, items in a listbox start at zero. We're going to be retrieving emails from the server. If you tried to retrieve email 0 you'd get an error.
The second line is this:
strRetrieve = "RETR " + index_number + vbCrLf
Or this, in C#:
strRetrieve = "RETR " + indexNumber + "\r\n";
This builds up a string for the POP3 command. The command we need is RETR. It works like this:
RETR 1
The 1 after RETR is the email number you want to retrieve. The server will then give you back the whole of the email. The first line of the server response will be something this, however:
+OK 1200 octets
So you get the +OK response followed by the size in octets of the email you are retrieving. The rest of the response is the email's header and the body.
After building up a string for the POP3 command, we have this line:
GetEmail( strRetrieve )
This is a Subroutine/method you haven't created yet. We've called the Sub GetEmail. In between the round brackets of the Sub/method, we're passing in the POP3 Command.
But the code for your SelectedIndexChanged event should look like this in VB Net:
C# coders, here's your SelectedIndexChanged event:
You now need to create the GetEmail Sub. Add the following code stub:
VB Net
Private Sub GetEmail(serverCommand As String )
End Sub
C#
private void GetEmail(string serverCommand)
{
}
In the code for the GetEmail Sub, we're going to be repeating
most of what our PopCommand function does. However, this function returns
a single line. The server response this time will be multiple lines.
Creating a new Sub to deal with just the email also means we can loop
through it, without cluttering up the PopCommand function. So add this
code to your new Sub in VB Net:
Dim serverBytes( ) As Byte = Encoding.ASCII.GetBytes(serverCommand)
Dim streamReader As StreamReader
Dim TextLine As String = ""
Try
networkStream.Write( serverBytes, 0, serverBytes.Length
)
streamReader = New StreamReader( networkStream )
Do While streamReader.Peek() <> -1
TextLine += streamReader.ReadLine( ) & vbNewLine
Loop
txtEmail.Text = TextLine
Catch ex As Exception
MessageBox.Show( ex.Message )
End Try
Add this to your method in C#:
try
{
Byte[] serverBytes = Encoding.ASCII.GetBytes(serverCommand);
StreamReader readStream;
string textLine = "";
networkStream.Write(serverBytes, 0, serverBytes.Length);
readStream = new StreamReader(networkStream);
while (readStream.Peek() != -1)
{
textLine = textLine + readStream.ReadLine() + "\r\n";
}
txtEmail.Text = textLine;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
You've already met most of this code when we set up the PopCommand function. The thing that's different is the Do While loop (just a while loop in C#):
Do While streamReader.Peek() <> -1
TextLine += streamReader.ReadLine( ) & vbNewLine
Loop
The Peek( ) <> -1 part looks one character ahead. If there are no more characters to be read then a value of -1 is returned. We're looping while the value is not -1.
Inside of the While loop, we're reading each line from the stream reader and building up the TextLine string variable.
After the loop has finished, we place the email contents into the text box:
txtEmail.Text = TextLine
Run your programme and test it out. Click your Connect button to check your emails. Now select an email from the ListBox at the top. You should see the entire contents of your email appear in the text box at the bottom of the form:
Now that we have an email in a text box, you might want to parse that email. In the next lesson, we'll separate the email header from the email body.
Back to the Intermediate Programming Contents Page
Email us: enquiry at homeandlearn.co.uk