/** * This Java Source Code and its associated Class files are *
* (c) 1998 Battelle Pacific Northwest National Laboratories *
* For further information, contact : *
* Chris Parkinson * Environmental Molecular Sciences Laboratory * Battelle Pacific Northwest National Laboratories * Richland * WA 99352 * USA */ import java.util.*; import java.text.*; import java.awt.*; /** * * SendMail class ... extends from the CommunicateMail class to provide * method for sending an Email message via a SMTP mail server. *
* SMTP mail sender uses port 25 (the standard SMTP socket) * and adheres to TCP/SMTP specs (RFC 821) * * @version 2.0 August 1998 * @author Chris Parkinson */ public class SendMail extends CommunicateMail { private Email email; //The email message to send private String heloHost; //The server identity //////////////////////////////////////////////////////// // // Constructor // //////////////////////////////////////////////////////// /** * Construct a new SendMail object, by default setting the SMTP port to 25 * and the mail server to 'pnl.gov' */ public SendMail(Frame parentFrame) { super(parentFrame); setMailPort(25); //Set up some defaults setMailServer("pnl.gov"); } //////////////////////////////////////////////////////// // // Set and Get the email object to send // //////////////////////////////////////////////////////// /** * Set the email object to send *
* @param email the email object to send */ public void setEmail(Email email) { this.email = email; } /** * Get the email object to send *
* @return the email object to send
*/
public Email getEmail() {
return email;
}
////////////////////////////////////////////////////////
//
// Send the Message
//
////////////////////////////////////////////////////////
/**
* Send the message.
*/
public void mainMethod() {
//Check the validity of our data
isDataValid();
if (isCancelled())return;
//Open up the socket connection
setProgress("Connecting to server (" + getMailServer()+ ")...");
openConnection();
if (isCancelled())return;
//Start talking 'server talk'
setProgress("Waiting for response from server (" + getMailServer() + ")...");
greetServer(); //Greet the server
if (isCancelled())return;
sendHeader(); //Send the mail headers
if (isCancelled())return;
sendData(); //Send the data and quit commands
if (isCancelled())return;
sendQuit(); //Quit communication
if (isCancelled())return;
//Close the connection
closeConnection();
setProgress("Connection closed");
setProgress("Mail sent !");
}
////////////////////////////////////////////////////////
//
// Is our data valid
//
////////////////////////////////////////////////////////
/**
* Is our data valid
*/
private void isDataValid() {
if (getEmail()==null) {
setError("No Email Message to Send");
return;
}
String result = null;
if (getEmail().getMailMessage()==null)result= "No Mail Message";
if (getEmail().getMailMessage().equals(""))result= "No Mail Message";
if (getEmail().getMailSubject()==null)result= "No Mail Subject Line";
if (getEmail().getMailSubject().equals(""))result= "No Mail Subject Line";
if (getEmail().getMailTo()==null)result ="No Mail 'To' Address";
else if (getEmail().getMailTo().length<=0)result= "No Mail 'To' Address";
if (getEmail().getMailFrom() == null)result= "No Mail 'From' Address";
if (getEmail().getMailFrom().equals(""))result= "No Mail 'From' Address";
if (getMailServer()==null)result= "No Server Name";
if (result!=null)setError(result);
}
////////////////////////////////////////////////////////
//
// Greet the server
// The server should send a '220' service ready message
//
////////////////////////////////////////////////////////
/**
* Greet the Server
*/
private void greetServer() {
try {
String words[] = toMultiWord(getServerLine(true));
if (words[0].equals("220")==false) {
setError("Server not able to accept connection - Try again later");
return;
}
//Now we get the server's identity, which is the next word
heloHost = words[1];
}catch(Exception e) {
setError("Unknown reply from mail server");
return;
}
//Now skip any other 220 messages that are sent
//(this is just extra info from server that we cna ignore)
String input="";
while (input!=null)input = getServerLine(false);
}
////////////////////////////////////////////////////////
//
// Send the Headers to the message
//
////////////////////////////////////////////////////////
/**
* Send the headers
*/
private void sendHeader() {
//Say Helo back to server
sendToServer("HELO", heloHost, true);
if (isCancelled())return;
receiveFromServer("250");
if (isCancelled())return;
//Mail From
sendToServer("MAIL FROM:", getEmail().getMailFrom(), true);
if (isCancelled())return;
receiveFromServer("250");
if (isCancelled())return;
//Mail To (Can have multiple recipients here)
String to[] = getEmail().getMailTo();
for (int i =0; i