1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.net.*; |
6 |
import java.io.*; |
7 |
|
8 |
/** |
9 |
* The Simple Mail Transfer Protocol class. This class was borrowed |
10 |
* for the GJT, and scaled to do just what we require. |
11 |
* |
12 |
* @author $Author: tdb1 $ |
13 |
* @version $Id: Smtp.java,v 1.6 2001/05/29 17:02:35 tdb1 Exp $ |
14 |
*/ |
15 |
public class Smtp { |
16 |
|
17 |
//---FINAL ATTRIBUTES--- |
18 |
|
19 |
/** |
20 |
* The current CVS revision of this class |
21 |
*/ |
22 |
public static final String REVISION = "$Revision: 1.6 $"; |
23 |
|
24 |
//---STATIC METHODS--- |
25 |
|
26 |
//---CONSTRUCTORS--- |
27 |
|
28 |
/** |
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 |
|
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(_socket.getOutputStream(), true); |
50 |
_socketIn.readLine(); // get 220 welcome header |
51 |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
52 |
} |
53 |
|
54 |
//---PUBLIC METHODS--- |
55 |
|
56 |
/** |
57 |
* Closes down the connection to the server |
58 |
* |
59 |
* @throws IOException if the command fails |
60 |
*/ |
61 |
public void close() throws IOException { |
62 |
sendCommand("QUIT", 221); |
63 |
_socketIn.close(); |
64 |
_socketOut.close(); |
65 |
_socket.close(); |
66 |
} |
67 |
|
68 |
/** |
69 |
* Specify who the message is from |
70 |
* |
71 |
* @param sender the e-mail address of the sender |
72 |
* @throws IOException if the command fails |
73 |
*/ |
74 |
public void setSender(String sender) throws IOException { |
75 |
sendCommand("MAIL FROM: <" + sender + ">", 250); |
76 |
} |
77 |
|
78 |
/** |
79 |
* Specify who the message is to be sent to |
80 |
* |
81 |
* @param to the e-mail address of the receiver |
82 |
* @throws IOException if the command fails |
83 |
*/ |
84 |
public void setTo(String to) throws IOException { |
85 |
sendCommand("RCPT TO: <" + to + ">", 250); |
86 |
} |
87 |
|
88 |
/** |
89 |
* Gets the PrintWriter allowing data to be sent |
90 |
* directly to the SMTP server |
91 |
* |
92 |
* @return a reference to the PrintWriter |
93 |
* @throws IOException if the command fails |
94 |
*/ |
95 |
public PrintWriter getOutputStream() throws IOException { |
96 |
sendCommand("DATA", 354); |
97 |
return _socketOut; |
98 |
} |
99 |
|
100 |
/** |
101 |
* Completes and sends the current message |
102 |
* |
103 |
* @throws IOException if the command fails |
104 |
*/ |
105 |
public void sendMessage() throws IOException { |
106 |
sendCommand(".", 250); |
107 |
} |
108 |
|
109 |
/** |
110 |
* Sends a command to the server |
111 |
* |
112 |
* @param cmd The command to send |
113 |
* @param reply The expected reply-code |
114 |
* @throws IOException if the incorrect response code is received |
115 |
*/ |
116 |
public void sendCommand(String cmd, int reply) throws IOException { |
117 |
_socketOut.println(cmd); |
118 |
String temp = _socketIn.readLine(); |
119 |
if (temp == null) { |
120 |
throw new IOException ("IO error reading from socket, connection to server died?"); |
121 |
} |
122 |
if (!temp.startsWith(String.valueOf(reply))) { |
123 |
throw new IOException ("Expected " + reply + ", got " + temp); |
124 |
} |
125 |
} |
126 |
|
127 |
/** |
128 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
129 |
* method to provide clean logging (every class should have this). |
130 |
* |
131 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
132 |
* to format the toString() |
133 |
* |
134 |
* @return the name of this class and its CVS revision |
135 |
*/ |
136 |
public String toString() { |
137 |
return FormatName.getName( |
138 |
_name, |
139 |
getClass().getName(), |
140 |
REVISION); |
141 |
} |
142 |
|
143 |
//---PRIVATE METHODS--- |
144 |
|
145 |
//---ACCESSOR/MUTATOR METHODS--- |
146 |
|
147 |
//---ATTRIBUTES--- |
148 |
|
149 |
/** |
150 |
* This is the friendly identifier of the |
151 |
* component this class is running in. |
152 |
* eg, a Filter may be called "filter1", |
153 |
* If this class does not have an owning |
154 |
* component, a name from the configuration |
155 |
* can be placed here. This name could also |
156 |
* be changed to null for utility classes. |
157 |
*/ |
158 |
private String _name = null; |
159 |
|
160 |
/** |
161 |
* A reference to the Reader connected to the server |
162 |
*/ |
163 |
private BufferedReader _socketIn; |
164 |
|
165 |
/** |
166 |
* A reference to the Writer connected to the server |
167 |
*/ |
168 |
private PrintWriter _socketOut; |
169 |
|
170 |
/** |
171 |
* A reference to the Socket connected to the server |
172 |
*/ |
173 |
private Socket _socket; |
174 |
|
175 |
//---STATIC ATTRIBUTES--- |
176 |
|
177 |
} |
178 |
|