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.1 2001/02/05 03:14:15 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.1 $"; |
23 |
|
24 |
//---STATIC METHODS--- |
25 |
|
26 |
//---CONSTRUCTORS--- |
27 |
|
28 |
/** |
29 |
* Connects to the specified SMTP server |
30 |
* |
31 |
* @param server The SMTP server to use |
32 |
*/ |
33 |
public Smtp(String server) throws IOException { |
34 |
this(server, 25); |
35 |
} |
36 |
|
37 |
/** |
38 |
* Connects to the specified SMTP server on a given port |
39 |
* |
40 |
* @param server The SMTP server to use |
41 |
* @param port The SMTP server port |
42 |
*/ |
43 |
public Smtp(String server, int port) throws IOException { |
44 |
_socket = new Socket(server, port); |
45 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
46 |
_socketOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream())), true); |
47 |
_socketIn.readLine(); // get 220 welcome header |
48 |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
49 |
} |
50 |
|
51 |
//---PUBLIC METHODS--- |
52 |
|
53 |
/** |
54 |
* closes the connection |
55 |
*/ |
56 |
public void close() throws IOException { |
57 |
sendCommand("QUIT", 221); |
58 |
_socketIn.close(); |
59 |
_socketOut.close(); |
60 |
_socket.close(); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Who is this message from? specify with this command. |
65 |
*/ |
66 |
public void from(String from) throws IOException { |
67 |
sendCommand("MAIL FROM: <" + from + ">", 250); |
68 |
} |
69 |
|
70 |
/** |
71 |
* Who should this message go to? Specify with this command. |
72 |
*/ |
73 |
public void to(String to) throws IOException { |
74 |
sendCommand("RCPT TO: <" + to + ">", 250); |
75 |
} |
76 |
|
77 |
/** |
78 |
* gets the outputstream |
79 |
*/ |
80 |
public PrintWriter getOutputStream() throws IOException { |
81 |
sendCommand("DATA", 354); |
82 |
return _socketOut; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Sends current message. |
87 |
*/ |
88 |
public void sendMessage() throws IOException { |
89 |
sendCommand(".", 250); |
90 |
} |
91 |
|
92 |
/** |
93 |
* Sends a command to the server |
94 |
* |
95 |
* @param cmd The command to send |
96 |
* @param reply The expected reply-code |
97 |
*/ |
98 |
public void sendCommand(String cmd, int reply) throws IOException { |
99 |
_socketOut.println(cmd); |
100 |
String temp = _socketIn.readLine(); |
101 |
if (!temp.startsWith("" + reply)) { |
102 |
throw new IOException ("Expected " + reply + ", got " + temp); |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
108 |
* method to provide clean logging (every class should have this). |
109 |
* |
110 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
111 |
* to format the toString() |
112 |
* |
113 |
* @return the name of this class and its CVS revision |
114 |
*/ |
115 |
public String toString() { |
116 |
return FormatName.getName( |
117 |
_name, |
118 |
getClass().getName(), |
119 |
REVISION); |
120 |
} |
121 |
|
122 |
//---PRIVATE METHODS--- |
123 |
|
124 |
//---ACCESSOR/MUTATOR METHODS--- |
125 |
|
126 |
//---ATTRIBUTES--- |
127 |
|
128 |
/** |
129 |
* This is the friendly identifier of the |
130 |
* component this class is running in. |
131 |
* eg, a Filter may be called "filter1", |
132 |
* If this class does not have an owning |
133 |
* component, a name from the configuration |
134 |
* can be placed here. This name could also |
135 |
* be changed to null for utility classes. |
136 |
*/ |
137 |
private String _name = null; |
138 |
|
139 |
/** from the server */ |
140 |
private BufferedReader _socketIn; |
141 |
|
142 |
/** to the server */ |
143 |
private PrintWriter _socketOut; |
144 |
|
145 |
/** The socket to use for this connection */ |
146 |
private Socket _socket; |
147 |
|
148 |
//---STATIC ATTRIBUTES--- |
149 |
|
150 |
} |
151 |
|