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

(please I was asked to create a smtp client and server programs both need to sup

ID: 3799501 • Letter: #

Question

(please I was asked to create a smtp client and server programs both need to support multiple clients. I am not sure If what I did is a SMTP client/server. also, I do not know how to support multiple clientes. please, only implment what I am missing. do not create new class, just use the two that i have thanks.)

This is exactly what i need to to:

Write a SMTP client program and a SMTP server program to implement the following simplified SMTP protocol based on TCP service. Please make sure your program supports multiple clients. You must use the port assigned to you and you may hard code it in both client and server programs.

SMTP Client Program (NOT a TELNET client): 1. Display a message to ask the user to input the Host Name (or ip-address) of your SMTP server. 2. Buildup the TCP connection to your SMTP server with the Host Name input by User at the given port. Catch the exception, terminate the program, and display error messages on the standard output if any. Wait for, read, and display the “220” response from the SMTP server. 3. Display prompt messages on the standard output to ask the user to input sender’s email address, receiver’s email address, subject, and email contents, respectively. Since there may be multiple lines in email contents, the prompt message displayed by your Client program MUST prompt the ending signature pattern like “.” on a line by itself. 4. Use the user inputs collected above in Step 3 for the following 3-phase data transfer procedure (see step 3 on server side). In each of the following steps a. through e., display the RTT (round-trip-time) of each conversation in millisecond (e.g., RTT = 212.08 ms). a. Send the “HELO” command followed by sender’s mail server domain name (e.g., xyz.com) to the SMTP server program, wait for server’s response and display it on the standard output. b. Send the “MAIL FROM: ” command to SMTP server, wait for SMTP server’s response and display it on the standard output. c. Send the “RCPT TO: ” command to the SMTP server program, wait for SMTP server’s response and display it on the standard output. d. Send the “DATA” command to the SMTP server program, wait for SMTP server’s response and display it on the standard output. e. Send the Mail message to the SMTP server. The format of this Mail message MUST follow the format detailed on the slide titled “Mail message format”. Wait for SMTP server’s response and display it on the standard output. 5. Display a prompt message to ask the User whether to continue. If yes, repeat steps 3 through 5. Otherwise, send a “QUIT” command to the SMTP server, display SMTP Server’s response, close TCP connection, and terminate the Client program.

SMTP Server Program: (server’s ip or client’s ip can be its dns name below.) 1. Listen to the given port and wait for a connection request from a SMTP Client. 2. Create a new thread for every incoming TCP connection request from a SMTP client. Send the “220” response including server’s ip address or dns name to the SMTP client. 3. Implement the following 3-phase data transfer procedure (see step 4 on client side): a. Wait for, read, and display the “HELO” command from the SMTP client. If the incoming command is NOT HELO, sends “503 5.5.2 Send hello first” response to the SMTP client and repeat step 3.a. b. Send the “250 Hello ” response to the SMTP client. c. Wait for, read, and display the “MAIL FROM” command from the SMTP client. If the incoming command is NOT “MAIL FROM”, sends “503 5.5.2 Need mail command” response to the SMTP client and repeat step 3.c. d. Send the “250 2.1.0 Sender OK” response to the SMTP client. e. Wait for, read, and display the “RCPT TO” command from the SMTP client. If the incoming command is NOT “RCPT TO”, send “503 5.5.2 Need rcpt command” response to the SMTP client and repeat step 3.e. f. Send the “250 2.1.5 Recipient OK” response to the SMTP client. g. Wait for, read, and display the “DATA” command from the SMTP client. If the incoming command is NOT “DATA”, send “503 5.5.2 Need data command” response to the SMTP client and repeat step 3.g. h. Send the “354 Start mail input; end with .” response to the SMTP client. i. Wait for, read, and display the Mail message from the SMTP client line by line. (hint: “.” is the ending signature.) j. Send the “250 Message received and to be delivered” response to the SMTP client. 4. Repeat Step 3 until the “QUIT” command is read. Upon receiving “QUIT”, send the “221 closing connection” response to the SMTP client and go to Step 5. 5. Close all i/o streams and the TCP socket for THIS Client, and terminate the thread for THIS client.

And this is What I have done:

.

This is what I have done in 3 days. I need help with this.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Client {

public static BufferedReader in;
public static PrintStream out;

public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the HostName (or ip address) of your SMTP server");
String ipAddress = scanner.nextLine();

//Buildup the TCP connection to your smtp server
System.out.println("stablishing connection");
System.out.println("Initializing connection with the servidor.");
//if (true) {
try {
Long startTime = System.currentTimeMillis();
  
  
Socket socket = new Socket(ipAddress, 25);//aqio in port va ipaddress
Long endTime = System.currentTimeMillis();
System.out.println("RTT for socket connection: " + (endTime - startTime));
Long elapsedTime = endTime - startTime;
System.out.println("Connection stablished | Elapsed time that"
+ " that it took " + elapsedTime + " miliseconds");

InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
//printstream para guardar strings.podemos leer mjs de cliente

/**
* Wait for, read and display a 220 response from he SMTP server
*
*/
while (true) {
System.out.println("Digito mimensaje para mandarlo a servidor.| 220 smtp.server.com Simple Mail Transfer Service Ready ");
String mensagem = scanner.nextLine();

out.println(mensagem);

if ("FIM".equals(mensagem)) {
break;

}
mensagem = in.readLine();
System.out.println(
"message recivied from server: "
+ mensagem);
  
InetAddress localhost;
Long start = System.currentTimeMillis();
localhost = InetAddress.getLocalHost();
System.out.println("HELO " + localhost.getHostName());
String helo = scanner.nextLine();
out.println(helo);
helo = in.readLine();
System.out.println(
"mensagem received by server: "
+ helo);
  
Long end = System.currentTimeMillis();
System.out.println("RTT = : " + (end - start)+" ms");
Long elapsedTim = end - start;
System.out.println("Connection stablished | Elapsed time that"
+ " that it took " + elapsedTim + " miliseconds");
  
System.out.println("Enter sender's email address ");
String from = scanner.nextLine();
System.out.println(from);
System.out.println("MAIL FROM: <" + from + ">");
String mailfrom = scanner.nextLine();
out.println(mailfrom);

System.out.println("Enter receiver's email address");
String to = scanner.nextLine();
System.out.println(to);
System.out.println();
System.out.println("RCPT TO:<" + to + ">");
String recipientOK = scanner.nextLine();
out.println(recipientOK);

System.out.println("DATA");
String data = scanner.nextLine();//The DATA command starts the transfer of the message contents (body text, attachments etc)
out.println("DATA");
//String line;
//while ((line = in.readLine()) != null) {
//out.println(line);
//}//Si el mensaje que llego se server no estavacio
  
System.out.println("Enter the subject");
String subject = scanner.nextLine();
out.println(subject);

System.out.println("Enter email contents");
String content = scanner.nextLine();
out.println(content+ " On a line by itself");
  
//out.println(content + " On a line by itself");
System.out.println("we hit this point");
//Get system properties
Properties properties = System.getProperties();

//Setup mail server
//properties.setProperty(to, ipAddress);
properties.setProperty("mail.smtp.port", ipAddress);//It is going to connect from the port 25 to the ip givenS

//Get the default session object
//Session session = Session.getDefaultInstance(properties);
Session session = Session.getInstance(properties);

// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
  
/**
* You can use createTextMessage on javax.jms.Session, e.g.

String xml = ...
Session session = ...
Message message = session.createTextMessage(xml);
*/

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
//message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
//message.setSubject("This is the Subject Line!");
message.setSubject(subject);
//System.out.println("we hit 2 point y imprime el subject sera cierto ? " + subject);

// Now set the actual message
//message.setText("This is actual message");
message.setText(content);
//System.out.println("we hit 3 point y imprime el content sera cierto ? " + content);

// Send message to the server
//Transport.send(message);
Transport transport = session.getTransport();
transport.connect();
transport.send(message);
transport.close();
System.out.println("Sent message successfully....");
System.out.println("Este es el supuesto mensaje que se manda " + message);
  
//-------------------------------------------------------------------------------
System.out.println("Would you like to continue, if NO press QUIT ");
String quit = scanner.nextLine();
if("QUIT".equals(quit)){//if you want to continue repeat steps 3 to 5
//ve idea 6 del cuaderno
  
}
  
}

System.out.println("Encerrado conexao");
in.close();
out.close();
socket.close();
  

} catch (UnknownHostException e) {// esto es igual q cachar UnknownHostException
System.out.println("The given HostName (or ip address) of "
+ "your SMTP server, was not recognized " + ipAddress);
System.exit(0);//a nonzero indica abnormal condicion
}
  
  
//}//End of if

}//End OF main

}

=================================

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Server {

public static void main(String[] args) throws Exception {
System.out.println("Stablishing connection");
ServerSocket server = new ServerSocket(25);
System.out.println("guardando concexion.");
Socket socket = server.accept();
System.out.println("conexion establecida.");
  
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
  
BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
//printstream para guardar strings.podemos leer mjs de cliente
  
while (true){
String mensagem = in.readLine();
  
  
System.out.println(
"Mensagem recebida do client [" +
socket.getInetAddress().getHostName() +
"]: " +
mensagem);
if ("FIM".equals(mensagem)){
break;
}
out.println(mensagem);
  

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
  
  
}
  
System.out.println("Encerrado servidor");
in.close();
out.close();
socket.close();
System.out.println("Encerrado servidor");
server.close();

}
  
}

Explanation / Answer

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Client {

public static BufferedReader in;
public static PrintStream out;

public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the HostName (or ip address) of your SMTP server");
String ipAddress = scanner.nextLine();

//Buildup the TCP connection to your smtp server
System.out.println("stablishing connection");
System.out.println("Initializing connection with the servidor.");
//if (true) {
try {
Long startTime = System.currentTimeMillis();
  
  
Socket socket = new Socket(ipAddress, 25);//aqio in port va ipaddress
Long endTime = System.currentTimeMillis();
System.out.println("RTT for socket connection: " + (endTime - startTime));
Long elapsedTime = endTime - startTime;
System.out.println("Connection stablished | Elapsed time that"
+ " that it took " + elapsedTime + " miliseconds");

InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
//printstream para guardar strings.podemos leer mjs de cliente

/**
* Wait for, read and display a 220 response from he SMTP server
*
*/
while (true) {
System.out.println("Digito mimensaje para mandarlo a servidor.| 220 smtp.server.com Simple Mail Transfer Service Ready ");
String mensagem = scanner.nextLine();

out.println(mensagem);

if ("FIM".equals(mensagem)) {
break;

}
mensagem = in.readLine();
System.out.println(
"message recivied from server: "
+ mensagem);
  
InetAddress localhost;
Long start = System.currentTimeMillis();
localhost = InetAddress.getLocalHost();
System.out.println("HELO " + localhost.getHostName());
String helo = scanner.nextLine();
out.println(helo);
helo = in.readLine();
System.out.println(
"mensagem received by server: "
+ helo);
  
Long end = System.currentTimeMillis();
System.out.println("RTT = : " + (end - start)+" ms");
Long elapsedTim = end - start;
System.out.println("Connection stablished | Elapsed time that"
+ " that it took " + elapsedTim + " miliseconds");
  
System.out.println("Enter sender's email address ");
String from = scanner.nextLine();
System.out.println(from);
System.out.println("MAIL FROM: <" + from + ">");
String mailfrom = scanner.nextLine();
out.println(mailfrom);

System.out.println("Enter receiver's email address");
String to = scanner.nextLine();
System.out.println(to);
System.out.println();
System.out.println("RCPT TO:<" + to + ">");
String recipientOK = scanner.nextLine();
out.println(recipientOK);

System.out.println("DATA");
String data = scanner.nextLine();//The DATA command starts the transfer of the message contents (body text, attachments etc)
out.println("DATA");
//String line;
//while ((line = in.readLine()) != null) {
//out.println(line);
//}//Si el mensaje que llego se server no estavacio
  
System.out.println("Enter the subject");
String subject = scanner.nextLine();
out.println(subject);

System.out.println("Enter email contents");
String content = scanner.nextLine();
out.println(content+ " On a line by itself");
  
//out.println(content + " On a line by itself");
System.out.println("we hit this point");
//Get system properties
Properties properties = System.getProperties();

//Setup mail server
//properties.setProperty(to, ipAddress);
properties.setProperty("mail.smtp.port", ipAddress);//It is going to connect from the port 25 to the ip givenS

//Get the default session object
//Session session = Session.getDefaultInstance(properties);
Session session = Session.getInstance(properties);

// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
  
/**
* You can use createTextMessage on javax.jms.Session, e.g.

String xml = ...
Session session = ...
Message message = session.createTextMessage(xml);
*/

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
//message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
//message.setSubject("This is the Subject Line!");
message.setSubject(subject);
//System.out.println("we hit 2 point y imprime el subject sera cierto ? " + subject);

// Now set the actual message
//message.setText("This is actual message");
message.setText(content);
//System.out.println("we hit 3 point y imprime el content sera cierto ? " + content);

// Send message to the server
//Transport.send(message);
Transport transport = session.getTransport();
transport.connect();
transport.send(message);
transport.close();
System.out.println("Sent message successfully....");
System.out.println("Este es el supuesto mensaje que se manda " + message);
  
//-------------------------------------------------------------------------------
System.out.println("Would you like to continue, if NO press QUIT ");
String quit = scanner.nextLine();
if("QUIT".equals(quit)){//if you want to continue repeat steps 3 to 5
//ve idea 6 del cuaderno
  
}
  
}

System.out.println("Encerrado conexao");
in.close();
out.close();
socket.close();
  

} catch (UnknownHostException e) {// esto es igual q cachar UnknownHostException
System.out.println("The given HostName (or ip address) of "
+ "your SMTP server, was not recognized " + ipAddress);
System.exit(0);//a nonzero indica abnormal condicion
}
  
  
//}//End of if

}//End OF main

}

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Server {

public static void main(String[] args) throws Exception {
System.out.println("Stablishing connection");
ServerSocket server = new ServerSocket(25);
System.out.println("guardando concexion.");
Socket socket = server.accept();
System.out.println("conexion establecida.");
  
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
  
BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
//printstream para guardar strings.podemos leer mjs de cliente
  
while (true){
String mensagem = in.readLine();
  
  
System.out.println(
"Mensagem recebida do client [" +
socket.getInetAddress().getHostName() +
"]: " +
mensagem);
if ("FIM".equals(mensagem)){
break;
}
out.println(mensagem);
  

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
  
  
}
  
System.out.println("Encerrado servidor");
in.close();
out.close();
socket.close();
System.out.println("Encerrado servidor");
server.close();

}
  
}