1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.6 |
package uk.org.iscream.cms.server.util; |
3 |
tdb |
1.1 |
|
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 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
13 |
tdb |
1.6 |
* @version $Id: Smtp.java,v 1.5 2001/03/14 23:25:29 tdb1 Exp $ |
14 |
tdb |
1.1 |
*/ |
15 |
|
|
public class Smtp { |
16 |
|
|
|
17 |
|
|
//---FINAL ATTRIBUTES--- |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* The current CVS revision of this class |
21 |
|
|
*/ |
22 |
tdb |
1.6 |
public static final String REVISION = "$Revision: 1.5 $"; |
23 |
tdb |
1.1 |
|
24 |
|
|
//---STATIC METHODS--- |
25 |
|
|
|
26 |
|
|
//---CONSTRUCTORS--- |
27 |
|
|
|
28 |
|
|
/** |
29 |
tdb |
1.3 |
* Connects to the specified SMTP server, on the default |
30 |
|
|
* (and standard) port 25. |
31 |
tdb |
1.1 |
* |
32 |
|
|
* @param server The SMTP server to use |
33 |
tdb |
1.3 |
* @throws IOException if the connection to the server fails |
34 |
tdb |
1.1 |
*/ |
35 |
|
|
public Smtp(String server) throws IOException { |
36 |
|
|
this(server, 25); |
37 |
|
|
} |
38 |
tdb |
1.3 |
|
39 |
tdb |
1.1 |
/** |
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 |
tdb |
1.3 |
* @throws IOException if the connection to the server fails |
45 |
tdb |
1.1 |
*/ |
46 |
|
|
public Smtp(String server, int port) throws IOException { |
47 |
|
|
_socket = new Socket(server, port); |
48 |
|
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
49 |
tdb |
1.3 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
50 |
tdb |
1.2 |
_socketIn.readLine(); // get 220 welcome header |
51 |
tdb |
1.1 |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
//---PUBLIC METHODS--- |
55 |
|
|
|
56 |
|
|
/** |
57 |
tdb |
1.3 |
* Closes down the connection to the server |
58 |
|
|
* |
59 |
|
|
* @throws IOException if the command fails |
60 |
tdb |
1.1 |
*/ |
61 |
|
|
public void close() throws IOException { |
62 |
|
|
sendCommand("QUIT", 221); |
63 |
|
|
_socketIn.close(); |
64 |
|
|
_socketOut.close(); |
65 |
|
|
_socket.close(); |
66 |
|
|
} |
67 |
tdb |
1.3 |
|
68 |
tdb |
1.1 |
/** |
69 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
74 |
tdb |
1.3 |
public void setSender(String sender) throws IOException { |
75 |
|
|
sendCommand("MAIL FROM: <" + sender + ">", 250); |
76 |
tdb |
1.1 |
} |
77 |
tdb |
1.3 |
|
78 |
tdb |
1.1 |
/** |
79 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
84 |
tdb |
1.3 |
public void setTo(String to) throws IOException { |
85 |
tdb |
1.1 |
sendCommand("RCPT TO: <" + to + ">", 250); |
86 |
|
|
} |
87 |
tdb |
1.3 |
|
88 |
tdb |
1.1 |
/** |
89 |
tdb |
1.3 |
* 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 |
tdb |
1.1 |
*/ |
95 |
|
|
public PrintWriter getOutputStream() throws IOException { |
96 |
|
|
sendCommand("DATA", 354); |
97 |
|
|
return _socketOut; |
98 |
|
|
} |
99 |
tdb |
1.3 |
|
100 |
tdb |
1.1 |
/** |
101 |
tdb |
1.3 |
* Completes and sends the current message |
102 |
|
|
* |
103 |
|
|
* @throws IOException if the command fails |
104 |
tdb |
1.1 |
*/ |
105 |
|
|
public void sendMessage() throws IOException { |
106 |
|
|
sendCommand(".", 250); |
107 |
|
|
} |
108 |
tdb |
1.3 |
|
109 |
tdb |
1.1 |
/** |
110 |
|
|
* Sends a command to the server |
111 |
|
|
* |
112 |
|
|
* @param cmd The command to send |
113 |
|
|
* @param reply The expected reply-code |
114 |
tdb |
1.3 |
* @throws IOException if the incorrect response code is received |
115 |
tdb |
1.1 |
*/ |
116 |
|
|
public void sendCommand(String cmd, int reply) throws IOException { |
117 |
|
|
_socketOut.println(cmd); |
118 |
|
|
String temp = _socketIn.readLine(); |
119 |
tdb |
1.4 |
if (!temp.startsWith(new Integer(reply).toString())) { |
120 |
tdb |
1.1 |
throw new IOException ("Expected " + reply + ", got " + temp); |
121 |
|
|
} |
122 |
|
|
} |
123 |
tdb |
1.3 |
|
124 |
tdb |
1.1 |
/** |
125 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
|
|
* method to provide clean logging (every class should have this). |
127 |
|
|
* |
128 |
tdb |
1.6 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
129 |
tdb |
1.1 |
* to format the toString() |
130 |
|
|
* |
131 |
|
|
* @return the name of this class and its CVS revision |
132 |
|
|
*/ |
133 |
|
|
public String toString() { |
134 |
|
|
return FormatName.getName( |
135 |
|
|
_name, |
136 |
|
|
getClass().getName(), |
137 |
|
|
REVISION); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
//---PRIVATE METHODS--- |
141 |
|
|
|
142 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
143 |
|
|
|
144 |
|
|
//---ATTRIBUTES--- |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* This is the friendly identifier of the |
148 |
|
|
* component this class is running in. |
149 |
|
|
* eg, a Filter may be called "filter1", |
150 |
|
|
* If this class does not have an owning |
151 |
|
|
* component, a name from the configuration |
152 |
|
|
* can be placed here. This name could also |
153 |
|
|
* be changed to null for utility classes. |
154 |
|
|
*/ |
155 |
|
|
private String _name = null; |
156 |
tdb |
1.3 |
|
157 |
|
|
/** |
158 |
|
|
* A reference to the Reader connected to the server |
159 |
|
|
*/ |
160 |
tdb |
1.1 |
private BufferedReader _socketIn; |
161 |
tdb |
1.3 |
|
162 |
|
|
/** |
163 |
|
|
* A reference to the Writer connected to the server |
164 |
|
|
*/ |
165 |
tdb |
1.1 |
private PrintWriter _socketOut; |
166 |
tdb |
1.3 |
|
167 |
|
|
/** |
168 |
|
|
* A reference to the Socket connected to the server |
169 |
|
|
*/ |
170 |
tdb |
1.1 |
private Socket _socket; |
171 |
|
|
|
172 |
|
|
//---STATIC ATTRIBUTES--- |
173 |
|
|
|
174 |
|
|
} |
175 |
|
|
|