Email and Web Lab HTTP and SMTP Lab This lab is divided into three parts. In the first part, you use telnet to manually connect to any web server and request a document from that server. In the second part you manually send mail through an SMTP mail server. In the final part, you write a Java program that performs the same action. It is recommended that you use a Linux Terminal session to carry out the first two parts of the lab. Part 1: Connecting to a Web Server with Telnet The aim of this exercise is to use Telnet to manually connect to a web server of your choice and request a file (in much the same way as an internet browser does). When you do this you will recieve a response from the server depending upon your request and you may analyse this response in order to further your understanding of how web clients and web servers work and the implications of the protocol used. In order to connect to a webserver using Telnet you must first start a Windows "Command Prompt" (CMD) session or a Linux "Terminal" session. From here you can use Telnet to specify a web server to connect to on a specified port number (port 80 is the default HTTP server socket). The following example shows you how to do this and the format of the command: telnet cgi.csc.liv.ac.uk 80 From here you will recieve confirmation from the server that you are connected succesfully/unsuccesfully. If connected succesfully you may then retrieve a document from the server by specifying the protocol keyword used (in this case it is "GET", as you are retrieving a document), the path name that the file is located in and the protocol you are going to use in order to do this. The following example shows how you would do this using CMD/Terminal:
GET /~mlk/ HTTP/1.1
Host: cgi.csc.liv.ac.uk
Take a look at the output and try the following: Use HTTP/1.0 instead of 1.1 and compare the output. Try looking for a non-existing path and analyse the output Try contacting another webserver and retrieving a document You will notice that there is header information contained in the response from the server when you request a document. Is the length of the document defined anywhere? Part 2: Sending Email with Telnet For this section you should try to send an email to yourself, this means you need to know the host name of the mail server for your mail domain (mail.csc.liv.ac.uk in our case). The following command will establish a TCP connection to this mail server (notice that the port number has changed as we need to use SMTP). telnet mail.csc.liv.ac.uk 25 At this point, the telnet program will allow you to enter SMTP commands, and will display the responses from the mail server. For example, the following sequence of commands would send email to bob from alice HELO alice
MAIL FROM:
RCPT TO:
DATA
SUBJECT: hello
Hi Bob, How's the weather? Alice.
.
QUIT
The SMTP protocol was originally designed to allow people to manually interact with mail servers in a conversational manner. For this reason, if you enter a command with incorrect syntax, or with unacceptable arguments, the server will return a message stating this, and will allow you to try again. To complete this part of the lab, you should send an email message to yourself and verify that it was delivered. Part 3: Sending Email with Java Java provides an API for interacting with the Internet mail system, which is called JavaMail. However, we will not be using this API, because it hides the details of SMTP and socket programming. Instead, you should write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. You can place all of your code into the main method of a class called EmailSender. Run your program with the following simple command: java EmailSender
This means you will include in your code the details of the particular email message you are trying to send. Here is a skeleton of the code you'll need to write: import java.io.*;
import java.net.*;
public class EmailSender
{
public static void main(String[] args) throws Exception
{
// Establish a TCP connection with the mail server.
// Create a BufferedReader to read a line at a time.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Read greeting from the server.
String response = br.readLine();
System.out.println(response);
if (!response.startsWith("220")) {
throw new Exception("220 reply not received from server.");
}
// Get a reference to the socket's output stream.
OutputStream os = socket.getOutputStream();
// Send HELO command and get server response.
String command = "HELO alice\r\n";
System.out.print(command);
os.write(command.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250")) {
throw new Exception("250 reply not received from server.");
}
// Send MAIL FROM command.
// Send RCPT TO command.
// Send DATA command.
// Send message data.
// End with line with a single period.
// Send QUIT command.
}
}