Deleting Emails from the Server

Receive Email Walkthrough: for C# and VB NET Students


In the previous lessons you've learnt how to download and read emails from a server. But you can also delete emails that are on the POP3 server. For this, the DELE command is used:


The number after DELE is the email number you want to delete.

The problem we face is that emails are not actually deleted when you issue the DELE command - they are only marked for deletion. They only get deleted when you issue the QUIT command. But we can indicate that an email is marked for deletion by adding text to the item that was selected in the ListBox. Because our Quit Button is issuing the QUIT command, the email will get deleted after the BtnQuit is clicked. Let's see how it works.

So open up the code stub for your Delete button. The first thing to do is to get which item was selected in the ListBox. You can this with the selected index property:

VB Net

Dim i As Integer = ListBox1.SelectedIndex + 1


int i = listBox1.SelectedIndex + 1;

Again, we're adding 1 to the selected index. This is because we need to get a message number to delete, and ListBoxes start counting at zero.

Now add the following code in VB Net:


Dim serverResponse As String
serverResponse = PopCommand( networkStream, "DELE " & i.ToString )
txtServerResponse.Text += serverResponse & vbCrLf

ListBox1.Items.Item( i - 1 ) += " Marked for Deletion"

txtEmail.Text = ""

Catch ex As Exception


End Try

And this in C#;


string serverResponse;
serverResponse = PopCommand(networkStream, "DELE " + i.ToString());
txtServerResponse.Text += serverResponse + "\r\n";
listBox1.Items[i - 1] += " Marked for Deletion";

txtEmail.Text = "";

catch (Exception ex)



Inside the Try … Catch block, we send the DELE command to the POP3 server:

serverResponse = PopCommand( networkStream, "DELE " & i.ToString )

The i variable holds which email number needs deleting. This has to be converted to a string.

The next line just adds the response to the textbox, so that we can see what came back from the server.

To mark the email as "for deletion", we have this:

ListBox1.Items.Item(i - 1) += " Marked for Deletion"

All we're doing here is adding some text to what is already there for that Item in the listbox. The final line just clears the text box at the bottom of the form.

Your Delete button code should look like this in VB Net:

VB NET  code issuing the DELE command to an email server

And this in C#:

VB NET  code issuing the DELE command to an email server

Before you run your programme, send yourself at least two emails. Now run your programme and click Connect. Then select an email. Click your Delete button and you should see something like this. Note the Server Response:

An email marked for deletion on the server

Even though the response is "+OK Deleted", the email has not actually been deleted. To finalise things at the server level, you would click your Quit button.

However, click on a second email, and then back on the email that is marked for deletion. You should see an error message appear in your Email text box at the bottom.

The reason for the error is that emails marked for deletion can no longer be read. But what if you made a mistake and actually want to read it again? In which case, you can reset the server.


Reset the Pop3 Server

If you want to go back and unmark an email you had previously marked for deletion, you can issue the RSET command. This, you won't be surprised to hear, stands for Reset.

Add another button to your form, and change the Name property to btnReset. Add some text to your button and then double click it to get at the code stub. Now enter the following code:

VB Net

Dim serverResponse As String = PopCommand( network_stream, "RSET" )
txtServerResponse.Text += serverResponse & vbCrLf


string serverResponse = PopCommand( network_stream, "RSET" );
txtServerResponse.Text += serverResponse + "\r\n";

All the code does, then, is to send the RSET command to the server.

Try it out. Mark an email for deletion. Then click your Reset button. Your Server Response text box will say something like RESURRECTED.


And that's it for the email client walkthrough. It's tricky stuff. But you should be at an intermediate level now, in your programming. So this is the kind of project you need to tackle and understand.

Back to the C# NET Contents Page

Back to the VB NET Contents Page