This assignment was a lot of fun…
The first assignment of 2nd year was to implement an Email Client with Browser Capabilities. This was a task which, at first (and second) glance appeared unfathomable – especially as we had only 3 weeks in which to complete it! Fortunately, the skeleton code we were given had a lot of pointers to help us to make a start on the process.
There were 2 main parts to the assignment: Establishing and using a mail server connection (sending mail), and downloading from an HTTP server via a TCP connection (downloading and sending objects). We could also implement our own choice of optional extra functionality.
Opening an SMTP (Simple Mail Transfer Protocol) connection requires some Socket programming. Sockets are a feature of the java.net package which are used to open a connection to a server on the client side (coming soon: using ServerSocket (server side connection)! watch this space…). A TCP socket is created by supplying the destination host (with which the connection is being established) and the port number (to which server side process the data transferred in the connection is passed). I used the University mail server “mail.csc.liv.ac.uk” and port 25 for the assignment when using the University machines, and “localhost” and port 1025 when using my own machine loaded with fake SMTP server software.
A BufferedReader and a DataOutputStream are then ‘attached’ to the socket to allow transferral of information between client and server. An important part of establishing connections to servers is understanding response codes. A successful connection is OK’d by the server with the code ‘220’. Once this has been confirmed, the SMTP commands can be used, beginning with a handshake.
A ‘handshake’ (in computing) is an initial connection in which the ‘rules’ for the connection are agreed. These rules include information such as verfication rules, the speed of the connection, and authorization. In SMTP connections, the handshake can be initiated by the client using the command:
HELO (don’t ask) followed by some name by which the client addresses the server.
All being well, the server responds with a 250 response code and a greeting for the client.
MAIL FROM firstname.lastname@example.org
is the SMTP command used to communicate to the server that the client wishes to send an email using the address email@example.com.
This is then OK’d with a 250 and a confirmation of that email address as valid.
The next command is RCPT TO firstname.lastname@example.org, which gives the address to which the mail will be sent. A 250 response is sent again, with confirmation od this email address.
The content of the mssage to be sent is begun with the command DATA, which is responded to with a 354 code. Anything written between the receipt of this code and a line containing only a full stop is the message content. (The command SUBJECT: can be used within this area to determine the subject line of the email.) A 250 response should then be received and the message is sent.
The client responds with QUIT to end the SMTP connection, which triggers a 221 response.
This is the order of SMTP commands for sending mail, in this assignment a user interface was available to us, where the input could be inserted as fields of a form. All I had to do was take the contents of these fields and use them following the appropriate command.