Implementing POP3

5.4.1 Implementing POP3

Like SMTP, POP3 is a command-line-based protocol, where each line is terminated with a line-feed ( <enter> ) character. For variable length lines, the command is terminated by <enter>.<enter> as in SMTP.

When the server is operating normally, each line will start with +OK . If an error occurs, the line begins with –ERR <some explanation> . Once the client establishes a TCP connection to the server on port 110, the server will always reply with +OK <some greeting message><enter> .

Chapter 5

142 5.4 Post office protocol 3

To access a mailbox, the client must authenticate itself with a username and password. The client sends USER <username><enter> . The server then replies with +OK <welcome><enter> . The password is sent as USER <pass-

word ><enter> with the same response from the server.

To get summary information about the mailbox, the command STAT<enter> is issued. To this the server will reply +OK <number of mes- sages > <total size><enter> . Unlike the previous messages, where the text after the +OK could be ignored, here it must be read and stored for future use.

To read back an email, the client sends the RETR <number> command; Number must be between 1 and the number received in response to the STAT command. The server will respond +OK <some message><enter><mail

body ><enter>.<enter> . The only piece of important information is the mail body; everything else can be ignored.

To delete emails, the client sends the DELE <number> command. The server will respond +OK <some message><enter> . At this point, it is possi- ble simply to close the TCP connection, but it is recommended to send QUIT<enter> .

To illustrate the protocol more simply, the following text shows the chain of events that occur between a POP3 server and client. As before, “S” indicates a transmission from server to client, and “C” indicates a client-to- server transaction. Here, user Bob is checking his emails, when he receives two messages from Alice and Terry.

S: +OK POP3 server ready

C: USER bob

S: +OK user valid

C: PASS secret

S: +OK pass valid

C: STAT

S: +OK 2 170

C: RETR 1

S: +OK 120 octets S: hello, how are you bob?, haven’t seen you in S: ages, any chance you could give me a call S: sometime? I’d love to see you. Alice S: .

C: DELE 1

S: +OK message 1 deleted

5.4 Post office protocol 3 143

C: RETR 2

S: +OK 50 octets S: Hi bob, I got the order of 500 widgets placed S: with Acme. Terry S: .

C: DELE 2

S: +OK message 2 deleted

C: QUIT

S: +OK

This transcript has been simplified for reasons of clarity. Modern mail messages contain headers, including the subject, date, natural names of the sender and recipient, and technical information concerning what software was used to send the email and how it was relayed.

This is a message header sent from fiach_reid@hotmail.com to fiach@eir- com.net.

Return-Path: <fiach_reid@hotmail.com> Delivered-To: eircom.net-fiach@eircom.net Received: (vpopmail 31497 invoked by uid 16); 11 Jan 2004

21:51:58 +0000 Received: (qmail 31491 messnum 229855 invoked from

network[64.4.19.76/law12-f76.law12.hotmail.com]); 11 Jan 2004 21:51:57 -0000

Received: from law12-f76.law12.hotmail.com (HELO hotmail.com) (64.4.19.76)

by mail09.svc.cra.dublin.eircom.net (qp 31491) with SMTP; 11 Jan 2004 21:51:57 -0000

Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;

Sun, 11 Jan 2004 13:51:56 -0800 Received: from 195.92.168.176 by lw12fd.law12.hotmail.msn.com

with HTTP; Sun, 11 Jan 2004 21:51:56 GMT X-Originating-IP: [195.92.168.176] X-Originating-Email: [fiach_reid@hotmail.com] X-Sender: fiach_reid@hotmail.com From: "Fiach Reid" <fiach_reid@hotmail.com> To: fiach@eircom.net Bcc: Subject: test message

Chapter 5

144 5.4 Post office protocol 3

Date: Sun, 11 Jan 2004 21:51:56 +0000 Mime-Version: 1.0 Status: U X-UIDL:

1073857917.31497.mail09.svc.cra.dublin.eircom.net,S=1118 Content-Type: text/plain; format=flowed Message-ID: <Law12-F76F1HkikieqX000054e5@hotmail.com> X-OriginalArrivalTime: 11 Jan 2004 21:51:56.0469 (UTC)

FILETIME=[21BF7650:01C3D88D] Two line-feed characters separate the message header from the body.

Example: POP3 client SPAM filter

SPAM is the term used for mass, unsolicited email. These emails are some- times accompanied by attached viruses, which can be accidentally opened by unwitting users. This application could be used to safely delete emails containing message fragments indicative of a SPAM email; in this case, the string “free money.”

This simple program scans your mailbox for emails containing the text “free money” and deletes them. This is obviously overly simplistic, but the example is here for illustration, not practicality.

The first step is to draw the user interface; you will need three textboxes, labeled tbServer , tbUsername , and tbPassword . Another textbox is required, named tbStatus ; this textbox should be set with multiline to true . Finally, place a button on the form, and call it btnClean .

First, import the required namespaces:

C#

using System.Threading; using System.Net; using System.Net.Sockets; using System.Text; using System.IO;