1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* 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 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.util; |
22 |
> |
package uk.org.iscream.cms.server.util; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
|
import java.net.*; |
26 |
|
import java.io.*; |
27 |
|
|
28 |
|
/** |
29 |
< |
* The Simple Mail Transfer Protocol class. This class was borrowed |
30 |
< |
* for the GJT, and scaled to do just what we require. |
29 |
> |
* The Simple Mail Transfer Protocol class. This class was inspired |
30 |
> |
* by the GJT. |
31 |
|
* |
32 |
|
* @author $Author$ |
33 |
|
* @version $Id$ |
46 |
|
//---CONSTRUCTORS--- |
47 |
|
|
48 |
|
/** |
49 |
< |
* Connects to the specified SMTP server |
49 |
> |
* Connects to the specified SMTP server, on the default |
50 |
> |
* (and standard) port 25. |
51 |
|
* |
52 |
|
* @param server The SMTP server to use |
53 |
+ |
* @throws IOException if the connection to the server fails |
54 |
|
*/ |
55 |
|
public Smtp(String server) throws IOException { |
56 |
|
this(server, 25); |
57 |
|
} |
58 |
< |
|
58 |
> |
|
59 |
|
/** |
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 |
+ |
* @throws IOException if the connection to the server fails |
65 |
|
*/ |
66 |
|
public Smtp(String server, int port) throws IOException { |
67 |
|
_socket = new Socket(server, port); |
68 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
69 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
70 |
< |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
69 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
70 |
> |
init(); |
71 |
|
} |
72 |
|
|
73 |
|
//---PUBLIC METHODS--- |
74 |
|
|
75 |
|
/** |
76 |
< |
* closes the connection |
76 |
> |
* Closes down the connection to the server |
77 |
> |
* |
78 |
> |
* @throws IOException if the command fails |
79 |
|
*/ |
80 |
|
public void close() throws IOException { |
81 |
|
sendCommand("QUIT", 221); |
83 |
|
_socketOut.close(); |
84 |
|
_socket.close(); |
85 |
|
} |
86 |
< |
|
86 |
> |
|
87 |
|
/** |
88 |
< |
* Who is this message from? specify with this command. |
88 |
> |
* 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 |
|
*/ |
93 |
< |
public void from(String from) throws IOException { |
94 |
< |
sendCommand("MAIL FROM: <" + from + ">", 250); |
93 |
> |
public void setSender(String sender) throws IOException { |
94 |
> |
sendCommand("MAIL FROM: <" + sender + ">", 250); |
95 |
|
} |
96 |
< |
|
96 |
> |
|
97 |
|
/** |
98 |
< |
* Who should this message go to? Specify with this command. |
98 |
> |
* 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 |
|
*/ |
103 |
< |
public void to(String to) throws IOException { |
103 |
> |
public void setTo(String to) throws IOException { |
104 |
|
sendCommand("RCPT TO: <" + to + ">", 250); |
105 |
|
} |
106 |
< |
|
106 |
> |
|
107 |
|
/** |
108 |
< |
* gets the outputstream |
108 |
> |
* 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 |
|
*/ |
114 |
|
public PrintWriter getOutputStream() throws IOException { |
115 |
|
sendCommand("DATA", 354); |
116 |
|
return _socketOut; |
117 |
|
} |
118 |
< |
|
118 |
> |
|
119 |
|
/** |
120 |
< |
* Sends current message. |
120 |
> |
* Completes and sends the current message |
121 |
> |
* |
122 |
> |
* @throws IOException if the command fails |
123 |
|
*/ |
124 |
|
public void sendMessage() throws IOException { |
125 |
|
sendCommand(".", 250); |
126 |
|
} |
127 |
< |
|
127 |
> |
|
128 |
|
/** |
129 |
|
* Sends a command to the server |
130 |
|
* |
131 |
|
* @param cmd The command to send |
132 |
|
* @param reply The expected reply-code |
133 |
+ |
* @throws IOException if the incorrect response code is received |
134 |
|
*/ |
135 |
|
public void sendCommand(String cmd, int reply) throws IOException { |
136 |
|
_socketOut.println(cmd); |
137 |
|
String temp = _socketIn.readLine(); |
138 |
< |
if (!temp.startsWith("" + reply)) { |
138 |
> |
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 |
|
throw new IOException ("Expected " + reply + ", got " + temp); |
143 |
|
} |
144 |
|
} |
145 |
< |
|
145 |
> |
|
146 |
|
/** |
147 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
148 |
|
* method to provide clean logging (every class should have this). |
149 |
|
* |
150 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
150 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
151 |
|
* to format the toString() |
152 |
|
* |
153 |
|
* @return the name of this class and its CVS revision |
161 |
|
|
162 |
|
//---PRIVATE METHODS--- |
163 |
|
|
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 |
+ |
|
184 |
|
//---ACCESSOR/MUTATOR METHODS--- |
185 |
|
|
186 |
|
//---ATTRIBUTES--- |
195 |
|
* be changed to null for utility classes. |
196 |
|
*/ |
197 |
|
private String _name = null; |
198 |
< |
|
199 |
< |
/** from the server */ |
198 |
> |
|
199 |
> |
/** |
200 |
> |
* A reference to the Reader connected to the server |
201 |
> |
*/ |
202 |
|
private BufferedReader _socketIn; |
203 |
< |
|
204 |
< |
/** to the server */ |
203 |
> |
|
204 |
> |
/** |
205 |
> |
* A reference to the Writer connected to the server |
206 |
> |
*/ |
207 |
|
private PrintWriter _socketOut; |
208 |
< |
|
209 |
< |
/** The socket to use for this connection */ |
208 |
> |
|
209 |
> |
/** |
210 |
> |
* A reference to the Socket connected to the server |
211 |
> |
*/ |
212 |
|
private Socket _socket; |
213 |
|
|
214 |
|
//---STATIC ATTRIBUTES--- |