26 |
|
//---CONSTRUCTORS--- |
27 |
|
|
28 |
|
/** |
29 |
< |
* Connects to the specified SMTP server |
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 |
< |
|
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(new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream())), true); |
49 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
50 |
|
_socketIn.readLine(); // get 220 welcome header |
51 |
|
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
52 |
|
} |
54 |
|
//---PUBLIC METHODS--- |
55 |
|
|
56 |
|
/** |
57 |
< |
* closes the connection |
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); |
64 |
|
_socketOut.close(); |
65 |
|
_socket.close(); |
66 |
|
} |
67 |
< |
|
67 |
> |
|
68 |
|
/** |
69 |
< |
* Who is this message from? specify with this command. |
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 from(String from) throws IOException { |
75 |
< |
sendCommand("MAIL FROM: <" + from + ">", 250); |
74 |
> |
public void setSender(String sender) throws IOException { |
75 |
> |
sendCommand("MAIL FROM: <" + sender + ">", 250); |
76 |
|
} |
77 |
< |
|
77 |
> |
|
78 |
|
/** |
79 |
< |
* Who should this message go to? Specify with this command. |
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 to(String to) throws IOException { |
84 |
> |
public void setTo(String to) throws IOException { |
85 |
|
sendCommand("RCPT TO: <" + to + ">", 250); |
86 |
|
} |
87 |
< |
|
87 |
> |
|
88 |
|
/** |
89 |
< |
* gets the outputstream |
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 |
< |
|
99 |
> |
|
100 |
|
/** |
101 |
< |
* Sends current message. |
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 |
< |
|
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("" + reply)) { |
119 |
> |
if (!temp.startsWith(new String(reply))) { |
120 |
|
throw new IOException ("Expected " + reply + ", got " + temp); |
121 |
|
} |
122 |
|
} |
123 |
< |
|
123 |
> |
|
124 |
|
/** |
125 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
|
* method to provide clean logging (every class should have this). |
153 |
|
* be changed to null for utility classes. |
154 |
|
*/ |
155 |
|
private String _name = null; |
156 |
< |
|
157 |
< |
/** from the server */ |
156 |
> |
|
157 |
> |
/** |
158 |
> |
* A reference to the Reader connected to the server |
159 |
> |
*/ |
160 |
|
private BufferedReader _socketIn; |
161 |
< |
|
162 |
< |
/** to the server */ |
161 |
> |
|
162 |
> |
/** |
163 |
> |
* A reference to the Writer connected to the server |
164 |
> |
*/ |
165 |
|
private PrintWriter _socketOut; |
166 |
< |
|
167 |
< |
/** The socket to use for this connection */ |
166 |
> |
|
167 |
> |
/** |
168 |
> |
* A reference to the Socket connected to the server |
169 |
> |
*/ |
170 |
|
private Socket _socket; |
171 |
|
|
172 |
|
//---STATIC ATTRIBUTES--- |