--- projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2001/02/05 04:03:07 1.2 +++ projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2001/02/05 20:46:28 1.3 @@ -10,7 +10,7 @@ import java.io.*; * for the GJT, and scaled to do just what we require. * * @author $Author: tdb $ - * @version $Id: Smtp.java,v 1.2 2001/02/05 04:03:07 tdb Exp $ + * @version $Id: Smtp.java,v 1.3 2001/02/05 20:46:28 tdb Exp $ */ public class Smtp { @@ -19,31 +19,34 @@ public class Smtp { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.2 $"; + public static final String REVISION = "$Revision: 1.3 $"; //---STATIC METHODS--- //---CONSTRUCTORS--- /** - * Connects to the specified SMTP server + * Connects to the specified SMTP server, on the default + * (and standard) port 25. * * @param server The SMTP server to use + * @throws IOException if the connection to the server fails */ public Smtp(String server) throws IOException { this(server, 25); } - + /** * Connects to the specified SMTP server on a given port * * @param server The SMTP server to use * @param port The SMTP server port + * @throws IOException if the connection to the server fails */ public Smtp(String server, int port) throws IOException { _socket = new Socket(server, port); _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); - _socketOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream())), true); + _socketOut = new PrintWriter(_socket.getOutputStream(), true); _socketIn.readLine(); // get 220 welcome header sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); } @@ -51,7 +54,9 @@ public class Smtp { //---PUBLIC METHODS--- /** - * closes the connection + * Closes down the connection to the server + * + * @throws IOException if the command fails */ public void close() throws IOException { sendCommand("QUIT", 221); @@ -59,50 +64,63 @@ public class Smtp { _socketOut.close(); _socket.close(); } - + /** - * Who is this message from? specify with this command. + * Specify who the message is from + * + * @param sender the e-mail address of the sender + * @throws IOException if the command fails */ - public void from(String from) throws IOException { - sendCommand("MAIL FROM: <" + from + ">", 250); + public void setSender(String sender) throws IOException { + sendCommand("MAIL FROM: <" + sender + ">", 250); } - + /** - * Who should this message go to? Specify with this command. + * Specify who the message is to be sent to + * + * @param to the e-mail address of the receiver + * @throws IOException if the command fails */ - public void to(String to) throws IOException { + public void setTo(String to) throws IOException { sendCommand("RCPT TO: <" + to + ">", 250); } - + /** - * gets the outputstream + * Gets the PrintWriter allowing data to be sent + * directly to the SMTP server + * + * @return a reference to the PrintWriter + * @throws IOException if the command fails */ public PrintWriter getOutputStream() throws IOException { sendCommand("DATA", 354); return _socketOut; } - + /** - * Sends current message. + * Completes and sends the current message + * + * @throws IOException if the command fails */ public void sendMessage() throws IOException { sendCommand(".", 250); } - + /** * Sends a command to the server * * @param cmd The command to send * @param reply The expected reply-code + * @throws IOException if the incorrect response code is received */ public void sendCommand(String cmd, int reply) throws IOException { _socketOut.println(cmd); String temp = _socketIn.readLine(); - if (!temp.startsWith("" + reply)) { + if (!temp.startsWith(new String(reply))) { throw new IOException ("Expected " + reply + ", got " + temp); } } - + /** * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). @@ -135,14 +153,20 @@ public class Smtp { * be changed to null for utility classes. */ private String _name = null; - - /** from the server */ + + /** + * A reference to the Reader connected to the server + */ private BufferedReader _socketIn; - - /** to the server */ + + /** + * A reference to the Writer connected to the server + */ private PrintWriter _socketOut; - - /** The socket to use for this connection */ + + /** + * A reference to the Socket connected to the server + */ private Socket _socket; //---STATIC ATTRIBUTES---