Email Spoofing HomeWork


  1. Your program must not take user input (although you can do that during testing). It must only send a single pre-defined email.
  2. Test your program using your own email address. After you are done testing and once you are ready to submit, change the email address in the RCPT TO line to “”. Do not test your program with this recipient address.
  3. Submit only a single source file (not a compiled file) for the entire team to the collab website. This file should compile or run without requiring an IDE or any other files. You should use Python3 or Java, and you only need to use one of the template files provided.
  4. The DATA in the message should be the names of each person on the team, one name per line. Be sure all names are included, as credit will not be given otherwise.
  5. Write all the group members’ names at the top of the source code. Also include any details on reading, compiling, or interpreting your code in the same comment area.
  6. I suggest that you use on port 25 as your mail server. You are also recommended against sending messages off-grounds or to a gmail address. NOTE: you are recommended to test/debug on grounds. If you work off-grounds, you will need to use your ISP’s mail server and may get “relay” error messages.
  7. The mail server you use must be a fully qualified domain name.
  8. If you are using Java, your code should compile with $ javac If you are using Python: $ python3

Project 1: Sending an Email

This project is divided into two parts. In the first part, you use telnet to manually send mail through an SMTP mail server. In the second part, you write code to perform the same action. While the first part is not graded, it is recommended that you do it as it makes the second part easier.

Part 1: Sending Email with Telnet

When you do this project, you should try to send an email to yourself. This means you need to know the hostname of the mail server for your mail domain. To find out this information, you can query DNS for the MX record that maintains information about your mail domain. For example, has mail domain The following command queries DNS for the mail servers responsible for delivering mail to this domain:

$ nslookup -type=MX

For the response to this command, there may be several mail servers that will deliver mail to mailboxes in the domain Suppose that the name of one of them is In this case, the following command will establish a TCP connection to this mail server. (Notice that the port number 25 is specified on the command line.)

telnet 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 Marques from Charlotte:

HELO Charlotte
SUBJECT: hello
Hi Marques, How's the weather? Charlotte.

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 project, you should send an email message to yourself and verify that it was delivered.

Part 2: Sending Email with Code

Programming languages often provide libraries for sending an email using the SMTP protocol, however, we will not be using these! We will instead be using a TCP socket directly and manually interacting with an SMTP mail server.

Your code should establish a TCP connection with the mail server, send the necessary commands to send a predefined email, and ensure that the correct response codes are received from the mail server.

You may write your code in Java or Python3. See the code snippets below.

#!/usr/bin/env python3

# Include needed libraries. Do _not_ include any libraries not included with
# Python3 (i.e. do not use `pip`).
import socket

# Establish a TCP connection with the mail server.

# Read greeting from the server
data = s.recv(BUFFER_SIZE)
response = data.decode('utf-8')

if not response.startswith('220'):
	raise Exception('220 reply not received from server.')

# Send HELO command and get server response.
cmd_HELO = 'HELO alice\r\n'

response = s.recv(4096).decode('utf-8')

if not response.startswith('250'):
    raise 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.

# Close the socket when finished.


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();

      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";

      response = br.readLine();

      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.