Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mail User Agent- Simplified Version In this assignment, you will write a Java pr

ID: 3735840 • Letter: M

Question

Mail User Agent- Simplified Version

In this assignment, you will write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body.

Here is a skeleton of the code you'll need to write:

Socket Web Programming – Single Thread Web Server

Your target is to develop a single thread web server that is capable of serving one request and response at a time. Your program will establish a separate TCP connection for each request/response pair, respond to correct GET request and send back information that the client requests, reject all other type of request by send a http 400 bad request response. If the file is not found, send a http 404 file not found response. You may only support text/html and image/jpeg file type.

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

import java.io.*;
import java.net.*;
import java.util.*;

public class EmailAgent {

   /**
   * @param args the command line arguments
   */
   public static void main(String[] args) throws Exception{
       // TODO code application logic here
       // Establish a TCP connection with the mail server.
       System.out.println("Enter the mail server to connect to ");
       String hostName = new String();
       Scanner emailScanner = new Scanner(System.in);
       hostName = emailScanner.next();
       Socket emailSocket = new Socket(hostName, 25);

       InputStream is = emailSocket.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. ");
       }

       OutputStream os = emailSocket.getOutputStream();

       System.out.println("Enter the name of your email domain:");
       String heloDomain = emailScanner.next();
       String fullHeloCommand = "HELO ALICE ";
       System.out.print(fullHeloCommand);
       os.write(fullHeloCommand.getBytes("US-ASCII"));
       response = br.readLine();
       System.out.println(response);
       if (!response.startsWith("250")) {
           throw new Exception("250 reply not received from server. ");
       }

       System.out.println("Enter your e-mail address ");
       String sourceAddress = emailScanner.next();
       String mailFromCommand = "MAIL FROM: <" + sourceAddress + "> ";
       System.out.println(mailFromCommand);
       os.write(mailFromCommand.getBytes("US-ASCII"));
       response = br.readLine();
       System.out.println(response);
       if (!response.startsWith("250"))
           throw new Exception("250 reply not received from server. ");

       System.out.println("Enter the destination e-mail address ");
       String destEmailAddress = new String();
       destEmailAddress = emailScanner.next();
       String fullAddress = new String();
       fullAddress = "RCPT TO: <" + destEmailAddress + "> ";
       System.out.println(fullAddress);
       os.write(fullAddress.getBytes("US-ASCII"));
       response = br.readLine();
       System.out.println(response);
       if(!response.startsWith("250"))
       {
           throw new Exception("250 reply not received from server. ");
       }

       String dataString = new String();
       dataString = "DATA";
       System.out.println(dataString);
       os.write(dataString.getBytes("US-ASCII"));
       response = br.readLine();
       if(!response.startsWith("354"))
           throw new Exception("354 reply not received from server. ");
       System.out.println(response);
       // Send message data.
       System.out.println("Enter your message ");
       String input = new String();
       while(input.charAt(0) != '.')
       {
           input = emailScanner.next();
           os.write(input.getBytes("US-ASCII"));
       }

       os.write(input.getBytes("US-ASCII"));
       response = br.readLine();
       System.out.println(response);
       if(!response.startsWith("250"))
           throw new Exception("250 reply not received from server ");

       String quitCommand = new String();
       quitCommand = "QUIT";
       os.write(quitCommand.getBytes("US-ASCII"));

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote