1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.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.3 2001/02/05 20:46:28 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.3 $"; |
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.startsWith(new Integer(reply).toString())) { |
120 |
throw new IOException ("Expected " + reply + ", got " + temp); |
121 |
} |
122 |
} |
123 |
|
124 |
/** |
125 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
* method to provide clean logging (every class should have this). |
127 |
* |
128 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
129 |
* 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 |
|
157 |
/** |
158 |
* A reference to the Reader connected to the server |
159 |
*/ |
160 |
private BufferedReader _socketIn; |
161 |
|
162 |
/** |
163 |
* A reference to the Writer connected to the server |
164 |
*/ |
165 |
private PrintWriter _socketOut; |
166 |
|
167 |
/** |
168 |
* A reference to the Socket connected to the server |
169 |
*/ |
170 |
private Socket _socket; |
171 |
|
172 |
//---STATIC ATTRIBUTES--- |
173 |
|
174 |
} |
175 |
|