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

I need the code for SMTP connection class which is missing import java.net.*; im

ID: 3617650 • Letter: I

Question


I need the code for SMTP connection class which is missing

import java.net.*;

import java.io.*;

import java.util.*;

/**

* Open an SMTP connection to a remote machine and send onemail.

*

*/

public class SMTPConnection {

/* The socket to the server */

private Socket connection;

/* Streams for reading and writing the socket */

private BufferedReader fromServer;

private DataOutputStream toServer;

private static final int SMTP_PORT = 25;

private static final String CRLF = " ";

/* Are we connected? Used in close() to determine what to do.*/

private boolean isConnected = false;

/* Create an SMTPConnection object. Create the socket and the

associated streams. Initialize SMTP connection. */

public SMTPConnection(Envelope envelope) throws IOException {

// connection = /* Fill in */;

fromServer = /* Fill in */;

toServer = /* Fill in */;

/* Fill in */

/* Read a line from server and check that the reply code is

220.

If not, throw an IOException. */

/* Fill in */

/* SMTP handshake. We need the name of the local machine.

Send the appropriate SMTP handshake command. */

file:///D|/Downloads/Livros/computação/Computer%20Netw...0Approach%20Featuring%20the%20Internet/MailClient.html(6 of 8)20/11/2004 15:53:19

A Mail User Agent in Java

String localhost = /* Fill in */;

sendCommand( /* Fill in */ );

isConnected = true;

}

/* Send the message. Write the correct SMTP-commands in the

correct order. No checking for errors, just throw them to the

caller. */

public void send(Envelope envelope) throws IOException {

/* Fill in */

/* Send all the necessary commands to send a message. Call

sendCommand() to do the dirty work. Do _not_ catch the

exception thrown from sendCommand(). */

/* Fill in */

}

/* Close the connection. First, terminate on SMTP level, then

close the socket. */

public void close() {

isConnected = false;

try {

sendCommand( /* Fill in */ );

// connection.close();

} catch (IOException e) {

System.out.println("Unable to close connection: " + e);

isConnected = true;

}

}

/* Send an SMTP command to the server. Check that the replycode

is

what is is supposed to be according to RFC 821. */

private void sendCommand(String command, int rc) throws

IOException {

/* Fill in */

/* Write command to server and read reply from server. */

/* Fill in */

/* Fill in */

/* Check that the server's reply code is the same as the

parameter

rc. If not, throw an IOException. */

file:///D|/Downloads/Livros/computação/Computer%20Netw...0Approach%20Featuring%20the%20Internet/MailClient.html(7 of 8)20/11/2004 15:53:19

A Mail User Agent in Java

/* Fill in */

}

/* Parse the reply line from the server. Returns the replycode.

*/

private int parseReply(String reply) {

/* Fill in */

}

/* Destructor. Closes the connection if something bad happens.*/

protected void finalize() throws Throwable {

if(isConnected) {

close();

}

super.finalize();

}

}

Explanation / Answer

Dear... import java.net.*;

import java.io.*;

import java.util.*;

/**

* Open an SMTP connection to a remote machine and send onemail.

*

*/

public class SMTPConnection {

/* The socket to the server */

private Socket connection;

/* Streams for reading and writing the socket */

private BufferedReader fromServer;

private DataOutputStream toServer;

private static final int SMTP_PORT = 25;

private static final String CRLF = " ";

/* Are we connected? Used in close() to determine what to do.*/

private boolean isConnected = false;

/* Create an SMTPConnection object. Create the socket and the

associated streams. Initialize SMTP connection. */

public SMTPConnection(Envelope envelope) throws IOException {

connection = newSocket("smtp".concat(envelope.DestHost),SMTP_PORT);

fromServer = new BufferedReader(newInputStreamReader(connection.getInputStream()));
toServer = newDataOutputStream(connection.getOutputStream());

/* Read a line from server and check that the reply code is

If not, throw an IOException. */

String text =fromServer.readLine(); System.out.println(parseReply(text)); if(parseReply(text)!=220) throw new IOException(); System.out.println("Mailserver".concat("smtp".).concat(envelope.DestHost).concat("found."));
/* SMTP handshake. We need the name of the local machine.

Send the appropriate SMTP handshake command. */

A Mail User Agent in Java

String localhost = (InetAddress.getLocalHost()).getCanonicalHostName();

sendCommand(“HELO“.concat(localhost), 250);
isConnected = true;

}

/* Send the message. Write the correct SMTP-commands in the

correct order. No checking for errors, just throw them to the

caller. */

public void send(Envelope envelope) throws IOException {



/* Send all the necessary commands to send a message. Call

sendCommand() to do the dirty work. Do _not_ catch the

exception thrown from sendCommand(). */

sendCommand(“MAIL FROM:“.concat(envelope.Sender),250);
sendCommand(“RCPT TO:“.concat(envelope.Recipient),250);
sendCommand(“DATA”,354);
sendCommand(envelope.Message.toString().concat(CRLF).concat(“.”),250);

}

/* Close the connection. First, terminate on SMTP level, then

close the socket. */

public void close() {

isConnected = false;

try {

sendCommand( “QUIT”, 221);

connection.close();

} catch (IOException e) {

System.out.println("Unable to close connection: " + e);

isConnected = true;

}

}

/* Send an SMTP command to the server. Check that the replycode

is

what is is supposed to be according to RFC 821. */

private void sendCommand(String command, int rc) throws

IOException {

/* Write command to server and read reply from server. */
System.out.println(“Command to server: “ +command);
command = command.concat(CRLF); // adds newline at end ofcommand.
toServer.writeBytes(command);

/* Check that the server's reply code is the same as theparameter
rc. If not, throw an IOException. */
String text = fromServer.readLine();
System.out.println(“Reply from server: “ + text);
if (parseReply(text) != rc){
System.out.println(“reply codes do notmatch”);
throw new IOException();
}
}

/* Parse the reply line from the server. Returns the replycode.

*/

private int parseReply(String reply) {
String tmp = reply.substring(0,3); // takesthe first three digits of the string
int i= Integer.parseInt(tmp); // converts the sting to aninteger
return i;


}

/* Destructor. Closes the connection if something bad happens.*/

protected void finalize() throws Throwable {

if(isConnected) {

close();

}

super.finalize();

}

}