ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java (file contents):
Revision 1.2 by tdb, Mon Feb 5 04:03:07 2001 UTC vs.
Revision 1.8 by tdb, Fri Feb 8 16:09:27 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.util;
2 > package uk.org.iscream.cms.server.util;
3  
4   //---IMPORTS---
5   import java.net.*;
# Line 26 | Line 26 | public class Smtp {
26   //---CONSTRUCTORS---
27  
28      /**
29 <     * Connects to the specified SMTP server
29 >     * Connects to the specified SMTP server, on the default
30 >     * (and standard) port 25.
31       *
32       * @param server The SMTP server to use
33 +     * @throws IOException if the connection to the server fails
34       */
35      public Smtp(String server) throws IOException {
36          this(server, 25);
37      }
38 <
38 >    
39      /**
40       * Connects to the specified SMTP server on a given port
41       *
42       * @param server The SMTP server to use
43       * @param port The SMTP server port
44 +     * @throws IOException if the connection to the server fails
45       */
46      public Smtp(String server, int port) throws IOException {
47          _socket = new Socket(server, port);
48          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
49 <        _socketOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream())), true);
50 <        _socketIn.readLine(); // get 220 welcome header
48 <        sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
49 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
50 >        init();
51      }
52  
53   //---PUBLIC METHODS---
54  
55      /**
56 <     * closes the connection
56 >     * Closes down the connection to the server
57 >     *
58 >     * @throws IOException if the command fails
59       */
60      public void close() throws IOException {
61          sendCommand("QUIT", 221);
# Line 59 | Line 63 | public class Smtp {
63          _socketOut.close();
64          _socket.close();
65      }
66 <
66 >    
67      /**
68 <     * Who is this message from? specify with this command.
68 >     * Specify who the message is from
69 >     *
70 >     * @param sender the e-mail address of the sender
71 >     * @throws IOException if the command fails
72       */
73 <    public void from(String from) throws IOException {
74 <        sendCommand("MAIL FROM: <" + from + ">", 250);
73 >    public void setSender(String sender) throws IOException {
74 >        sendCommand("MAIL FROM: <" + sender + ">", 250);
75      }
76 <
76 >    
77      /**
78 <      * Who should this message go to? Specify with this command.
78 >      * Specify who the message is to be sent to
79 >      *
80 >      * @param to the e-mail address of the receiver
81 >      * @throws IOException if the command fails
82        */
83 <    public void to(String to) throws IOException {
83 >    public void setTo(String to) throws IOException {
84          sendCommand("RCPT TO: <" + to + ">", 250);
85      }
86 <
86 >    
87      /**
88 <      * gets the outputstream
88 >     * Gets the PrintWriter allowing data to be sent
89 >     * directly to the SMTP server
90 >     *
91 >     * @return a reference to the PrintWriter
92 >     * @throws IOException if the command fails
93       */
94      public PrintWriter getOutputStream() throws IOException {
95          sendCommand("DATA", 354);
96          return _socketOut;
97      }
98 <
98 >    
99      /**
100 <     * Sends current message.
100 >     * Completes and sends the current message
101 >     *
102 >     * @throws IOException if the command fails
103       */
104      public void sendMessage() throws IOException {
105          sendCommand(".", 250);
106      }
107 <
107 >    
108      /**
109       * Sends a command to the server
110       *
111       * @param cmd The command to send
112       * @param reply The expected reply-code
113 +     * @throws IOException if the incorrect response code is received
114       */
115      public void sendCommand(String cmd, int reply) throws IOException {
116          _socketOut.println(cmd);
117          String temp = _socketIn.readLine();
118 <        if (!temp.startsWith("" + reply)) {
118 >        if (temp == null) {
119 >            throw new IOException ("IO error reading from socket, connection to server died?");
120 >        }
121 >        if (!temp.startsWith(String.valueOf(reply))) {
122              throw new IOException ("Expected " + reply + ", got " + temp);
123          }
124      }
125 <
125 >    
126      /**
127       * Overrides the {@link java.lang.Object#toString() Object.toString()}
128       * method to provide clean logging (every class should have this).
129       *
130 <     * This uses the uk.ac.ukc.iscream.util.FormatName class
130 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
131       * to format the toString()
132       *
133       * @return the name of this class and its CVS revision
# Line 121 | Line 141 | public class Smtp {
141  
142   //---PRIVATE METHODS---
143  
144 +    /**
145 +     * Check the server sends a 220 message, and
146 +     * then send our HELO.
147 +     *
148 +     * @throws IOException if something goes wrong
149 +     */
150 +    private void init() throws IOException {
151 +        String temp = _socketIn.readLine();
152 +        // skip over any 220- lines
153 +        while(temp != null && temp.startsWith("220-")) {
154 +            temp = _socketIn.readLine();
155 +        }
156 +        // check if we got a 220 welcome header
157 +        if(temp == null || !temp.startsWith("220 ")) {
158 +            throw new IOException ("Server did not initialise with a 220 message");
159 +        }
160 +        // server gave a 220, it's ok to do a HELO
161 +        sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
162 +    }
163 +
164   //---ACCESSOR/MUTATOR METHODS---
165  
166   //---ATTRIBUTES---
# Line 135 | Line 175 | public class Smtp {
175       * be changed to null for utility classes.
176       */
177      private String _name = null;
178 <
179 <    /** from the server */
178 >    
179 >    /**
180 >     * A reference to the Reader connected to the server
181 >     */
182      private BufferedReader _socketIn;
183 <
184 <    /** to the server */
183 >    
184 >    /**
185 >     * A reference to the Writer connected to the server
186 >     */
187      private PrintWriter _socketOut;
188 <
189 <    /** The socket to use for this connection */
188 >    
189 >    /**
190 >     * A reference to the Socket connected to the server
191 >     */
192      private Socket _socket;
193      
194   //---STATIC ATTRIBUTES---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines