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.1 by tdb, Mon Feb 5 03:14:15 2001 UTC vs.
Revision 1.9 by tdb, Sat May 18 18:16:04 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * Copyright (C) 2000-2002 i-scream
4 + *
5 + * This program is free software; you can redistribute it and/or
6 + * modify it under the terms of the GNU General Public License
7 + * as published by the Free Software Foundation; either version 2
8 + * of the License, or (at your option) any later version.
9 + *
10 + * This program is distributed in the hope that it will be useful,
11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 + * GNU General Public License for more details.
14 + *
15 + * You should have received a copy of the GNU General Public License
16 + * along with this program; if not, write to the Free Software
17 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 + */
19 +
20   //---PACKAGE DECLARATION---
21 < package uk.ac.ukc.iscream.util;
21 > package uk.org.iscream.cms.server.util;
22  
23   //---IMPORTS---
24   import java.net.*;
25   import java.io.*;
26  
27   /**
28 < * The Simple Mail Transfer Protocol class. This class was borrowed
29 < * for the GJT, and scaled to do just what we require.
28 > * The Simple Mail Transfer Protocol class. This class was inspired
29 > * by the GJT.
30   *
31   * @author  $Author$
32   * @version $Id$
# Line 26 | Line 45 | public class Smtp {
45   //---CONSTRUCTORS---
46  
47      /**
48 <     * Connects to the specified SMTP server
48 >     * Connects to the specified SMTP server, on the default
49 >     * (and standard) port 25.
50       *
51       * @param server The SMTP server to use
52 +     * @throws IOException if the connection to the server fails
53       */
54      public Smtp(String server) throws IOException {
55          this(server, 25);
56      }
57 <
57 >    
58      /**
59       * Connects to the specified SMTP server on a given port
60       *
61       * @param server The SMTP server to use
62       * @param port The SMTP server port
63 +     * @throws IOException if the connection to the server fails
64       */
65      public Smtp(String server, int port) throws IOException {
66          _socket = new Socket(server, port);
67          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
68 <        _socketOut = new PrintWriter(_socket.getOutputStream());
69 <        sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
68 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
69 >        init();
70      }
71  
72   //---PUBLIC METHODS---
73  
74      /**
75 <     * closes the connection
75 >     * Closes down the connection to the server
76 >     *
77 >     * @throws IOException if the command fails
78       */
79      public void close() throws IOException {
80          sendCommand("QUIT", 221);
# Line 58 | Line 82 | public class Smtp {
82          _socketOut.close();
83          _socket.close();
84      }
85 <
85 >    
86      /**
87 <     * Who is this message from? specify with this command.
87 >     * Specify who the message is from
88 >     *
89 >     * @param sender the e-mail address of the sender
90 >     * @throws IOException if the command fails
91       */
92 <    public void from(String from) throws IOException {
93 <        sendCommand("MAIL FROM: <" + from + ">", 250);
92 >    public void setSender(String sender) throws IOException {
93 >        sendCommand("MAIL FROM: <" + sender + ">", 250);
94      }
95 <
95 >    
96      /**
97 <      * Who should this message go to? Specify with this command.
97 >      * Specify who the message is to be sent to
98 >      *
99 >      * @param to the e-mail address of the receiver
100 >      * @throws IOException if the command fails
101        */
102 <    public void to(String to) throws IOException {
102 >    public void setTo(String to) throws IOException {
103          sendCommand("RCPT TO: <" + to + ">", 250);
104      }
105 <
105 >    
106      /**
107 <      * gets the outputstream
107 >     * Gets the PrintWriter allowing data to be sent
108 >     * directly to the SMTP server
109 >     *
110 >     * @return a reference to the PrintWriter
111 >     * @throws IOException if the command fails
112       */
113      public PrintWriter getOutputStream() throws IOException {
114          sendCommand("DATA", 354);
115          return _socketOut;
116      }
117 <
117 >    
118      /**
119 <     * Sends current message.
119 >     * Completes and sends the current message
120 >     *
121 >     * @throws IOException if the command fails
122       */
123      public void sendMessage() throws IOException {
124          sendCommand(".", 250);
125      }
126 <
126 >    
127      /**
128       * Sends a command to the server
129       *
130       * @param cmd The command to send
131       * @param reply The expected reply-code
132 +     * @throws IOException if the incorrect response code is received
133       */
134      public void sendCommand(String cmd, int reply) throws IOException {
135          _socketOut.println(cmd);
136          String temp = _socketIn.readLine();
137 <        if (!temp.startsWith("" + reply)) {
137 >        if (temp == null) {
138 >            throw new IOException ("IO error reading from socket, connection to server died?");
139 >        }
140 >        if (!temp.startsWith(String.valueOf(reply))) {
141              throw new IOException ("Expected " + reply + ", got " + temp);
142          }
143      }
144 <
144 >    
145      /**
146       * Overrides the {@link java.lang.Object#toString() Object.toString()}
147       * method to provide clean logging (every class should have this).
148       *
149 <     * This uses the uk.ac.ukc.iscream.util.FormatName class
149 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
150       * to format the toString()
151       *
152       * @return the name of this class and its CVS revision
# Line 120 | Line 160 | public class Smtp {
160  
161   //---PRIVATE METHODS---
162  
163 +    /**
164 +     * Check the server sends a 220 message, and
165 +     * then send our HELO.
166 +     *
167 +     * @throws IOException if something goes wrong
168 +     */
169 +    private void init() throws IOException {
170 +        String temp = _socketIn.readLine();
171 +        // skip over any 220- lines
172 +        while(temp != null && temp.startsWith("220-")) {
173 +            temp = _socketIn.readLine();
174 +        }
175 +        // check if we got a 220 welcome header
176 +        if(temp == null || !temp.startsWith("220 ")) {
177 +            throw new IOException ("Server did not initialise with a 220 message");
178 +        }
179 +        // server gave a 220, it's ok to do a HELO
180 +        sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250);
181 +    }
182 +
183   //---ACCESSOR/MUTATOR METHODS---
184  
185   //---ATTRIBUTES---
# Line 134 | Line 194 | public class Smtp {
194       * be changed to null for utility classes.
195       */
196      private String _name = null;
197 <
198 <    /** from the server */
197 >    
198 >    /**
199 >     * A reference to the Reader connected to the server
200 >     */
201      private BufferedReader _socketIn;
202 <
203 <    /** to the server */
202 >    
203 >    /**
204 >     * A reference to the Writer connected to the server
205 >     */
206      private PrintWriter _socketOut;
207 <
208 <    /** The socket to use for this connection */
207 >    
208 >    /**
209 >     * A reference to the Socket connected to the server
210 >     */
211      private Socket _socket;
212      
213   //---STATIC ATTRIBUTES---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines