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
Revision: 1.12
Committed: Sun Aug 1 10:41:08 2004 UTC (19 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +3 -3 lines
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

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