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

Please submit the following items in a ZIP file. 1) Source code; 2) Instructions

ID: 3793185 • Letter: P

Question

Please submit the following items in a ZIP file.

1) Source code;

2) Instructions on how to install and run your program;

3) A brief design document explaining your solution.

In addition most SMTP servers ) require authentication before sending messages. You can either hard-

code theemail account’s authentication information into the source code, or create a dummy or a free

SMTP server (shown as follows)to test your program.

http://www.softstack.com/freesmtp.html

https://www.hmailserver.com/

http://sourceforge.net/directory/os:windows/freshness:recently

-updated/?q=smtp%20server

(There are a couple of options. It seems that SMTPMail is a viable option if you feel comfortable

with common line mode.)

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

simple

Java program that establishes a TCP

connection with a mail server through the socket interface, and sends an ema

il message.

You can place all of your code into the main method of a class called

EmailAgent

. Run your

program with the following simple command:

java EmailAgent

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 EmailAgent

{

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

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.

}

}

For this assignment, you are required to use command-

line-based Java and should not rely onany features provided by IDE such as NetBeans, Eclipse, etc. In your submission please send me

a standalone document named “

EmailAgent.java

”. No executables should be submitted. Nographical interface should be used by your program.

Explanation / Answer

FInd the source code of the program below :

package JavaEmailProgram;
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"));
  
}
}