COMP211: HTTP and SMTP Lab

This lab is divided into three parts. In the first part, you use nc 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 on LXFarm to carry out the first two parts of the lab.

Guides on how to

can be found on our intranet.

For the last part you will need a computer with an up-to-date JAVA JDK. For ease of installation, I recommend AdoptOpenJDK.
Please use the Remote Teaching Centre Service to work on a PC e.g. in George Hold, lab 2 or 3 if you struggle to install a JAVA JDK on your own machine. Please follow the guide on Accessing Remote Desktop Service on our intranet.


Part 1: Connecting to a Web Server with netcat (nc)

The aim of this exercise is to use nc 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.

Within MobaXterm, you can use nc (or netcat) 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:

nc comp211.gairing.com 80

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 /Welcome.html HTTP/1.1
Host: comp211.gairing.com

Hit Return twice and take a look at the output. To close the nc connection use CTRL-C.

Important Note:
Most HTTP servers will timeout quickly and close the TCP connection fast than you can type a Http request message. I suggest you prepare the request message in an editor and just copy and past it in.

Try the following:

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 netcat (nc)

For this section you should try to send an email using SMTP commands directly. For security reason, we will not use a live SMTP server. Instead we use MailCatcher (https://mailcatcher.me), a simple SMTP server that catches any message sent to it and displays it a web interface. MailCatcher is installed on a Google Cloud Server. The web interface can be accessed via http://35.246.112.180:1080.

To send emails to MailCatcher, you can use the SMTP server 35.246.112.180 on port 1025. Note that MailCatcher is not using the standard port number for SMTP, which would be 25.

nc 35.246.112.180 1025

At this point, the nc 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 an email to bob from alice

HELO alice
MAIL FROM: <sender@example.com>
RCPT TO: <receiver@example.org>
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 and verify in the web interface that it was delivered.


Part 3: Sending Email with Java

You should be able to do this part on your own computer if you have an up-to-date JAVA JDK installed (OpenJDK). Otherwise, you can use the Linux farm as above or use the Remote Teaching Centre Service to work on a PC e.g. in George Hold, lab 2 or 3.

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.

Within MobaXterm you can e.g. use gedit as an editor for your java files. You can place all of your code into the main method of a class called EmailSender. This means you will include in your code the details of the particular email message you are trying to send.

Run your program with the following simple command:

java EmailSender

Here is a skeleton of the code you'll need to write. You can download the skeleton java file here: EmailSender.java.

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.
      Socket socket = new Socket("35.246.112.180",1025);

      // 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.
      DataOutputStream os = new DataOutputStream(socket.getOutputStream());

      // Send HELO command and get server response.
      String command = "HELO alice\r\n";
      System.out.print(command);
      os.writeBytes(command);
      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.



   }
}