SMTP Connection JAVA

Here’s some of the code that I created for SMTPConnect.java in my computer networking class. It basically allows the use of a pre-programmed JAVA mail client to connect to a SMTP mail server and send an e-mail.

import java.net.*;
import java.io.*;
import java.util.*;
/**
* Open an SMTP connection to a remote machine and send one mail.
*/

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 = “\r\n”;

/* 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 = new Socket(“smtp.”.concat(envelope.DestHost), SMTP_PORT);
fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
toServer = new DataOutputStream(connection.getOutputStream());

/* Read a line from server and check that the reply code is 220.
If not, throw an IOException. */

String text = fromServer.readLine();
System.out.println(parseReply(text));
if (parseReply(text) != 220)
throw new IOException();
System.out.println(“Mail server “.concat(“smtp.”).concat(envelope.DestHost).concat(” found.”));

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

String localhost = (InetAddress.getLocalHost()).getCanonicalHostName();
System.out.println(“LOCALHOST: “+localhost);
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); // closes SMTP
connection.close(); // closes socket
} catch (IOException e) {
System.out.println(“Unable to close connection: “ + e);
isConnected = true;
}
}

/* Send an SMTP command to the server. Check that the reply code
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 of command.
toServer.writeBytes(command);

/* Check that the server’s reply code is the same as the
parameter 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 not match”);
throw new IOException();
}
}

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

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

/* Destructor. Closes the connection if something bad happens. */
protected void finalize() throws Throwable {
if(isConnected) {
close();
}
super.finalize();
}
}

This will then produce an output to the console as such:

Sending mail
220
Mail server smtp.someschool.edu found.
LOCALHOST: my-laptop IP ADDRESS: 127.0.1.1
Command to server: HELO my-laptop
Reply from server: 250 smtp.someschool.edu
Command to server: MAIL FROM: sender@someschool.edu
Reply from server: 250 Ok
Command to server: RCPT TO: recipient@someschool.edu
Reply from server: 250 Ok
Command to server: DATA
Reply from server: 354 End data with .
Command to server: From: sender@someschool.edu
To: recipient@someschool.edu
Subject: HELLO
Date: Wed, 17 Sep 2008 16:35:45 GMT

The body of the message will go in here.

.
Reply from server: 250 Ok: queued as 3EAC34DBF7
Command to server: QUIT
Reply from server: 221 Bye

Currently, the InetAddress will only pick up the name of the local machine on the loopback IP, rather than the internet IP. No big deal since it works and passes all assignment criteria.

.concat(arg) will add whatever the arg is and add it to the string prior to the command. The same function can be done with the + sign between the two items (if they are both strings)

There are two ways to insert the return line (CRLF). One is how I have it here, and the other is to simply add .concat(CRLF) to the end of each sendCommand(). and remove the newline addition code that I added in the sendCommand() function.
IE sendCommand(DATA.concat(CRLF),354);

The following are the commands that need to be sent to the server and the associated “everythings working good” codes.
DATA – 354 (bulk of the e-mail)
HELO – 250 (handshake. Note that there is only one L)
MAIL FROM: – 250 (sender)
QUIT – 221 (obvious)
RCPT TO: – 250 (recipient)
. -250 (single period on new line = end of data)

About these ads
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: