1 |
tdb |
1.9 |
/* |
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 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.6 |
package uk.org.iscream.cms.server.util; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.net.*; |
25 |
|
|
import java.io.*; |
26 |
|
|
|
27 |
|
|
/** |
28 |
tdb |
1.9 |
* The Simple Mail Transfer Protocol class. This class was inspired |
29 |
|
|
* by the GJT. |
30 |
tdb |
1.1 |
* |
31 |
tdb |
1.8 |
* @author $Author: tdb $ |
32 |
tdb |
1.9 |
* @version $Id: Smtp.java,v 1.8 2002/02/08 16:09:27 tdb Exp $ |
33 |
tdb |
1.1 |
*/ |
34 |
|
|
public class Smtp { |
35 |
|
|
|
36 |
|
|
//---FINAL ATTRIBUTES--- |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* The current CVS revision of this class |
40 |
|
|
*/ |
41 |
tdb |
1.9 |
public static final String REVISION = "$Revision: 1.8 $"; |
42 |
tdb |
1.1 |
|
43 |
|
|
//---STATIC METHODS--- |
44 |
|
|
|
45 |
|
|
//---CONSTRUCTORS--- |
46 |
|
|
|
47 |
|
|
/** |
48 |
tdb |
1.3 |
* Connects to the specified SMTP server, on the default |
49 |
|
|
* (and standard) port 25. |
50 |
tdb |
1.1 |
* |
51 |
|
|
* @param server The SMTP server to use |
52 |
tdb |
1.3 |
* @throws IOException if the connection to the server fails |
53 |
tdb |
1.1 |
*/ |
54 |
|
|
public Smtp(String server) throws IOException { |
55 |
|
|
this(server, 25); |
56 |
|
|
} |
57 |
tdb |
1.3 |
|
58 |
tdb |
1.1 |
/** |
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 |
tdb |
1.3 |
* @throws IOException if the connection to the server fails |
64 |
tdb |
1.1 |
*/ |
65 |
|
|
public Smtp(String server, int port) throws IOException { |
66 |
|
|
_socket = new Socket(server, port); |
67 |
|
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
68 |
tdb |
1.3 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
69 |
tdb |
1.8 |
init(); |
70 |
tdb |
1.1 |
} |
71 |
|
|
|
72 |
|
|
//---PUBLIC METHODS--- |
73 |
|
|
|
74 |
|
|
/** |
75 |
tdb |
1.3 |
* Closes down the connection to the server |
76 |
|
|
* |
77 |
|
|
* @throws IOException if the command fails |
78 |
tdb |
1.1 |
*/ |
79 |
|
|
public void close() throws IOException { |
80 |
|
|
sendCommand("QUIT", 221); |
81 |
|
|
_socketIn.close(); |
82 |
|
|
_socketOut.close(); |
83 |
|
|
_socket.close(); |
84 |
|
|
} |
85 |
tdb |
1.3 |
|
86 |
tdb |
1.1 |
/** |
87 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
92 |
tdb |
1.3 |
public void setSender(String sender) throws IOException { |
93 |
|
|
sendCommand("MAIL FROM: <" + sender + ">", 250); |
94 |
tdb |
1.1 |
} |
95 |
tdb |
1.3 |
|
96 |
tdb |
1.1 |
/** |
97 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
102 |
tdb |
1.3 |
public void setTo(String to) throws IOException { |
103 |
tdb |
1.1 |
sendCommand("RCPT TO: <" + to + ">", 250); |
104 |
|
|
} |
105 |
tdb |
1.3 |
|
106 |
tdb |
1.1 |
/** |
107 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
113 |
|
|
public PrintWriter getOutputStream() throws IOException { |
114 |
|
|
sendCommand("DATA", 354); |
115 |
|
|
return _socketOut; |
116 |
|
|
} |
117 |
tdb |
1.3 |
|
118 |
tdb |
1.1 |
/** |
119 |
tdb |
1.3 |
* Completes and sends the current message |
120 |
|
|
* |
121 |
|
|
* @throws IOException if the command fails |
122 |
tdb |
1.1 |
*/ |
123 |
|
|
public void sendMessage() throws IOException { |
124 |
|
|
sendCommand(".", 250); |
125 |
|
|
} |
126 |
tdb |
1.3 |
|
127 |
tdb |
1.1 |
/** |
128 |
|
|
* Sends a command to the server |
129 |
|
|
* |
130 |
|
|
* @param cmd The command to send |
131 |
|
|
* @param reply The expected reply-code |
132 |
tdb |
1.3 |
* @throws IOException if the incorrect response code is received |
133 |
tdb |
1.1 |
*/ |
134 |
|
|
public void sendCommand(String cmd, int reply) throws IOException { |
135 |
|
|
_socketOut.println(cmd); |
136 |
|
|
String temp = _socketIn.readLine(); |
137 |
tdb |
1.7 |
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 |
tdb |
1.1 |
throw new IOException ("Expected " + reply + ", got " + temp); |
142 |
|
|
} |
143 |
|
|
} |
144 |
tdb |
1.3 |
|
145 |
tdb |
1.1 |
/** |
146 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
147 |
|
|
* method to provide clean logging (every class should have this). |
148 |
|
|
* |
149 |
tdb |
1.6 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
150 |
tdb |
1.1 |
* to format the toString() |
151 |
|
|
* |
152 |
|
|
* @return the name of this class and its CVS revision |
153 |
|
|
*/ |
154 |
|
|
public String toString() { |
155 |
|
|
return FormatName.getName( |
156 |
|
|
_name, |
157 |
|
|
getClass().getName(), |
158 |
|
|
REVISION); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
//---PRIVATE METHODS--- |
162 |
tdb |
1.8 |
|
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 |
tdb |
1.1 |
|
183 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
184 |
|
|
|
185 |
|
|
//---ATTRIBUTES--- |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* This is the friendly identifier of the |
189 |
|
|
* component this class is running in. |
190 |
|
|
* eg, a Filter may be called "filter1", |
191 |
|
|
* If this class does not have an owning |
192 |
|
|
* component, a name from the configuration |
193 |
|
|
* can be placed here. This name could also |
194 |
|
|
* be changed to null for utility classes. |
195 |
|
|
*/ |
196 |
|
|
private String _name = null; |
197 |
tdb |
1.3 |
|
198 |
|
|
/** |
199 |
|
|
* A reference to the Reader connected to the server |
200 |
|
|
*/ |
201 |
tdb |
1.1 |
private BufferedReader _socketIn; |
202 |
tdb |
1.3 |
|
203 |
|
|
/** |
204 |
|
|
* A reference to the Writer connected to the server |
205 |
|
|
*/ |
206 |
tdb |
1.1 |
private PrintWriter _socketOut; |
207 |
tdb |
1.3 |
|
208 |
|
|
/** |
209 |
|
|
* A reference to the Socket connected to the server |
210 |
|
|
*/ |
211 |
tdb |
1.1 |
private Socket _socket; |
212 |
|
|
|
213 |
|
|
//---STATIC ATTRIBUTES--- |
214 |
|
|
|
215 |
|
|
} |
216 |
|
|
|