1 /**
2 * This Java Source Code and its associated Class files are
3 * <P>
4 * (c) 1998 Battelle Pacific Northwest National Laboratories
5 * <P>
6 * For further information, contact :
7 * <P>
8 * Chris Parkinson
9 * Environmental Molecular Sciences Laboratory
10 * Battelle Pacific Northwest National Laboratories
11 * Richland
12 * WA 99352
13 * USA
14 */
15
16
17 import java.util.*;
18 import java.text.*;
19 import java.awt.*;
20 /**
21 *
22 * SendMail class ... extends from the CommunicateMail class to provide
23 * method for sending an Email message via a SMTP mail server.
24 * <p>
25 * SMTP mail sender uses port 25 (the standard SMTP socket)
26 * and adheres to TCP/SMTP specs (RFC 821)
27 *
28 * @version 2.0 August 1998
29 * @author Chris Parkinson
30 */
31
32 public class SendMail extends CommunicateMail
33 {
34
35 private Email email; //The email message to send
36 private String heloHost; //The server identity
37
38 ////////////////////////////////////////////////////////
39 //
40 // Constructor
41 //
42 ////////////////////////////////////////////////////////
43 /**
44 * Construct a new SendMail object, by default setting the SMTP port to 25
45 * and the mail server to 'pnl.gov'
46 */
SendMail(Frame parentFrame)47 public SendMail(Frame parentFrame) {
48 super(parentFrame);
49 setMailPort(25); //Set up some defaults
50 setMailServer("pnl.gov");
51 }
52
53 ////////////////////////////////////////////////////////
54 //
55 // Set and Get the email object to send
56 //
57 ////////////////////////////////////////////////////////
58 /**
59 * Set the email object to send
60 * <P>
61 * @param email the email object to send
62 */
setEmail(Email email)63 public void setEmail(Email email) {
64 this.email = email;
65 }
66
67 /**
68 * Get the email object to send
69 * <P>
70 * @return the email object to send
71 */
getEmail()72 public Email getEmail() {
73 return email;
74 }
75
76
77
78
79 ////////////////////////////////////////////////////////
80 //
81 // Send the Message
82 //
83 ////////////////////////////////////////////////////////
84 /**
85 * Send the message.
86 */
mainMethod()87 public void mainMethod() {
88
89 //Check the validity of our data
90 isDataValid();
91 if (isCancelled())return;
92
93 //Open up the socket connection
94 setProgress("Connecting to server (" + getMailServer()+ ")...");
95 openConnection();
96 if (isCancelled())return;
97
98 //Start talking 'server talk'
99 setProgress("Waiting for response from server (" + getMailServer() + ")...");
100
101 greetServer(); //Greet the server
102 if (isCancelled())return;
103
104 sendHeader(); //Send the mail headers
105 if (isCancelled())return;
106
107 sendData(); //Send the data and quit commands
108 if (isCancelled())return;
109
110 sendQuit(); //Quit communication
111 if (isCancelled())return;
112
113 //Close the connection
114 closeConnection();
115 setProgress("Connection closed");
116 setProgress("Mail sent !");
117 }
118
119
120
121
122
123
124
125
126
127 ////////////////////////////////////////////////////////
128 //
129 // Is our data valid
130 //
131 ////////////////////////////////////////////////////////
132 /**
133 * Is our data valid
134 */
isDataValid()135 private void isDataValid() {
136
137 if (getEmail()==null) {
138 setError("No Email Message to Send");
139 return;
140 }
141 String result = null;
142 if (getEmail().getMailMessage()==null)result= "No Mail Message";
143 if (getEmail().getMailMessage().equals(""))result= "No Mail Message";
144 if (getEmail().getMailSubject()==null)result= "No Mail Subject Line";
145 if (getEmail().getMailSubject().equals(""))result= "No Mail Subject Line";
146 if (getEmail().getMailTo()==null)result ="No Mail 'To' Address";
147 else if (getEmail().getMailTo().length<=0)result= "No Mail 'To' Address";
148 if (getEmail().getMailFrom() == null)result= "No Mail 'From' Address";
149 if (getEmail().getMailFrom().equals(""))result= "No Mail 'From' Address";
150 if (getMailServer()==null)result= "No Server Name";
151
152 if (result!=null)setError(result);
153 }
154
155
156
157 ////////////////////////////////////////////////////////
158 //
159 // Greet the server
160 // The server should send a '220' service ready message
161 //
162 ////////////////////////////////////////////////////////
163 /**
164 * Greet the Server
165 */
greetServer()166 private void greetServer() {
167 try {
168 String words[] = toMultiWord(getServerLine(true));
169 if (words[0].equals("220")==false) {
170 setError("Server not able to accept connection - Try again later");
171 return;
172 }
173
174 //Now we get the server's identity, which is the next word
175 heloHost = words[1];
176 }catch(Exception e) {
177 setError("Unknown reply from mail server");
178 return;
179 }
180
181 //Now skip any other 220 messages that are sent
182 //(this is just extra info from server that we cna ignore)
183 String input="";
184 while (input!=null)input = getServerLine(false);
185 }
186
187
188 ////////////////////////////////////////////////////////
189 //
190 // Send the Headers to the message
191 //
192 ////////////////////////////////////////////////////////
193 /**
194 * Send the headers
195 */
sendHeader()196 private void sendHeader() {
197 //Say Helo back to server
198 sendToServer("HELO", heloHost, true);
199 if (isCancelled())return;
200 receiveFromServer("250");
201 if (isCancelled())return;
202
203
204 //Mail From
205 sendToServer("MAIL FROM:", getEmail().getMailFrom(), true);
206 if (isCancelled())return;
207 receiveFromServer("250");
208 if (isCancelled())return;
209
210 //Mail To (Can have multiple recipients here)
211 String to[] = getEmail().getMailTo();
212 for (int i =0; i<to.length; i++) {
213 sendToServer("RCPT TO:", to[i], true);
214 if (isCancelled())return;
215
216 //Reply to this can be '250' or '251', so we shall check to '25x'
217 receiveFromServer("25");
218 if (isCancelled())return;
219 }
220 //Mail CC (Can have multiple recipients here)
221 String cc[] = getEmail().getMailCC();
222 for (int i =0; i<cc.length; i++) {
223 sendToServer("RCPT TO:", cc[i], true);
224 if (isCancelled())return;
225
226 //Reply to this can be '250' or '251', so we shall check to '25x'
227 receiveFromServer("25");
228 if (isCancelled())return;
229 }
230
231 }
232
233
234 ////////////////////////////////////////////////////////
235 //
236 // Send the actual message data
237 //
238 ////////////////////////////////////////////////////////
239 /**
240 * Send the message data
241 */
sendData()242 private void sendData() {
243 //Tell server we are sending data over
244 sendToServer("DATA", null, true);
245 if (isCancelled())return;
246 receiveFromServer("354");
247 if (isCancelled())return;
248
249
250 //Protocol says that first should be the subject line
251 String[] lines = toMultiLine(getEmail().getMailSubject());
252 setProgress("Sending data to server.");
253 sendToServer("Subject:", lines[0], false);
254 if (isCancelled())return;
255
256
257 //Date line
258 Date now = new Date();
259 SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyy hh:mm:ss z");
260 String date = formatter.format(now);
261 setProgress("Sending data to server..");
262 sendToServer("Date:", date, false);
263 if (isCancelled())return;
264
265 //To list if they exist
266 lines = getEmail().getMailTo();
267 setProgress("Sending data to server.");
268 String tos="";
269 for (int i=0; i<lines.length; i++) {
270 if (tos.equals("")==false) tos= tos + ", ";
271 tos = tos + lines[i];
272 }
273 if (tos.equals("")==false) {
274 sendToServer( "To:", tos, false);
275 if (isCancelled())return;
276 }
277
278
279
280 //CC list if they exist
281 lines = getEmail().getMailCC();
282 setProgress("Sending data to server.");
283 String ccs="";
284 for (int i=0; i<lines.length; i++) {
285 if (ccs.equals("")==false) ccs= ccs + ", ";
286 ccs = ccs + lines[i];
287 }
288 if (ccs.equals("")==false) {
289 sendToServer( "cc:", ccs, false);
290 if (isCancelled())return;
291 }
292
293
294 //Alternative headers if they exist
295 lines = getEmail().getMailHeader();
296 setProgress("Sending data to server.");
297 for (int i=0; i<lines.length; i++) {
298 sendToServer( lines[i], "", false);
299 if (isCancelled())return;
300 }
301
302
303
304
305
306
307 //Now a blank line to separate the main message
308 setProgress("Sending data to server...");
309 sendToServer("", null, false); //Blank line
310 if (isCancelled())return;
311
312
313 //Now the main body of the message (Note any single '.' should be changed to '..')
314 lines = toMultiLine(getEmail().getMailMessage());
315 StringBuffer message = new StringBuffer("Sending data to server");
316 for (int i =0; i< lines.length; i++) {
317 message.append(".");
318 setProgress(message.toString());
319 sendToServer(lines[i], null, false);
320 if (isCancelled())return;
321 }
322
323 //End DATA section with '\r\n.\r\n'
324 sendToServer("\r\n.", null, false);
325 if (isCancelled())return;
326
327 //receiveFromServer("250"); //Seems to be unneccessary and locks up sometimes
328 //if (isCancelled())return;
329
330 setProgress("Sent data");
331 }
332
333 ////////////////////////////////////////////////////////
334 //
335 // Send a Quit message
336 //
337 ////////////////////////////////////////////////////////
338 /**
339 * Send a Quit Message
340 */
sendQuit()341 private void sendQuit() {
342 //Finally send a QUIT message to close the conversation
343 sendToServer("QUIT", null, true);
344 if (isCancelled())return;
345
346 //Commenting this out 'cos its hangs and seems to be unnecessary
347 // receiveFromServer("221");
348 // if (isCancelled())return;
349 }
350
351
352
353
354 }