--- projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2001/02/05 03:14:15 1.1 +++ projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2002/05/18 18:16:04 1.9 @@ -1,16 +1,35 @@ +/* + * i-scream central monitoring system + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.util; //---IMPORTS--- import java.net.*; import java.io.*; /** - * The Simple Mail Transfer Protocol class. This class was borrowed - * for the GJT, and scaled to do just what we require. + * The Simple Mail Transfer Protocol class. This class was inspired + * by the GJT. * * @author $Author: tdb $ - * @version $Id: Smtp.java,v 1.1 2001/02/05 03:14:15 tdb Exp $ + * @version $Id: Smtp.java,v 1.9 2002/05/18 18:16:04 tdb Exp $ */ public class Smtp { @@ -19,38 +38,43 @@ public class Smtp { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.1 $"; + public static final String REVISION = "$Revision: 1.9 $"; //---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(_socket.getOutputStream()); - sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); + _socketOut = new PrintWriter(_socket.getOutputStream(), true); + init(); } //---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); @@ -58,55 +82,71 @@ 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 == null) { + throw new IOException ("IO error reading from socket, connection to server died?"); + } + if (!temp.startsWith(String.valueOf(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). * - * This uses the uk.ac.ukc.iscream.util.FormatName class + * This uses the uk.org.iscream.cms.server.util.FormatName class * to format the toString() * * @return the name of this class and its CVS revision @@ -120,6 +160,26 @@ public class Smtp { //---PRIVATE METHODS--- + /** + * Check the server sends a 220 message, and + * then send our HELO. + * + * @throws IOException if something goes wrong + */ + private void init() throws IOException { + String temp = _socketIn.readLine(); + // skip over any 220- lines + while(temp != null && temp.startsWith("220-")) { + temp = _socketIn.readLine(); + } + // check if we got a 220 welcome header + if(temp == null || !temp.startsWith("220 ")) { + throw new IOException ("Server did not initialise with a 220 message"); + } + // server gave a 220, it's ok to do a HELO + sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); + } + //---ACCESSOR/MUTATOR METHODS--- //---ATTRIBUTES--- @@ -134,14 +194,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---