Hi All,
Here is a version of the
send mail applet without the
closable frame....You should only use
this as a guide, since a servlet does not have
a gui (normally)....
Regards,
- DL
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Date;
public class SendMail extends Applet {
//
// The e-mail address all messages are to be sent to; specified in HTML
//
String webmasterEmail = null;
String serverHostName = null;
boolean standalone = false;
int smtpPort = 25;
Socket socket;
PrintWriter ps;
InetAddress rina;
InetAddress lina;
Form form;
/**
* Initialize the applet.
*/
public void init() {
setBackground(Color.white);
form = new Form(this);
add(form);
setSize (600, 450);
if (serverHostName == null) serverHostName = getCodeBase().getHost();
if (webmasterEmail == null) webmasterEmail = getParameter("RECIPIENT");
}
/**
* Show status to the user.
*/
public void showStatus(String s) {
System.out.println(s);
if (standalone) return;
super.showStatus(s);
}
BufferedReader buf_reader;
/**
* Send an e-mail message.
*/
public void send() throws IOException, Exception {
// Open connection to SMTP server
socket = new Socket(serverHostName, smtpPort);
// Send the form contents as a message
try {
rina = socket.getInetAddress();
lina = rina.getLocalHost();
//
// Conversion of the PrintStream due to deprication.
// ps = new PrintStream(socket.getOutputStream());
//
// Print values and objects to a Writer.
//
ps = new PrintWriter (socket.getOutputStream());
//
// Conversion of the DataInputStream due to deprication.
// dis = new DataInputStream(socket.getInputStream());
//
// Use BufferedReader instead of DataInputStream.
//
buf_reader = new BufferedReader (new
InputStreamReader(socket.getInputStream()));
// Send message
sendline("HELO " + lina.toString());
sendline("MAIL FROM:" + form.email());
sendline("RCPT TO:" + webmasterEmail);
sendline("DATA");
sendline(form.message());
sendline(".");
}
catch (Exception ex) {
socket.close();
throw ex;
}
// Close connection
socket.close();
}
/**
* Send a line of data to the server, and retrieve the handshake
*/
void sendline(String data) throws IOException {
System.out.println("sendline out:" + data);
ps.println(data);
ps.flush();
String s = buf_reader.readLine();
System.out.println("sendline in:" + s);
}
/**
* Main routine, for standalone program execution
*/
public static void main(String args[]) {
SendMail ap = new SendMail();
// The server host will be the place running POP
// webmaster e-mail will be recipient
ap.serverHostName = "192.168.1.95";
ap.webmasterEmail = "lyon";
ap.standalone = true;
Frame fr = new Frame("SendMail");
ap.init();
fr.add("Center", ap);
fr.setSize (600, 450);
fr.show();
ap.start();
}
}
/**
* A form for obtaining user input. Customize this for your application,
just
* as you would customize an HTML form for a Web-based e-mail application.
*/
class Form extends Panel implements ActionListener {
SendMail applet;
// The form's elements...
Label nameLabel;
TextField nameField;
Label emailLabel;
TextField emailField;
Label orgLabel;
TextField orgField;
Label msgBodyLabel;
TextArea msgBodyArea;
Button sendButton;
/**
* The constructor
*/
public Form(SendMail ap) {
applet = ap;
setBackground(Color.white);
setLayout(new GridLayout(2, 1));
// Create a panel to put the text fields and button on
Panel p = new Panel();
p.setLayout(new GridLayout(8, 1));
// Instantiate all the elements, and add them to their containers...
p.add (sendButton = new Button("Send"));
sendButton.addActionListener (this);
p.add (nameLabel = new Label("Your Name:"));
p.add (nameField = new TextField(60));
p.add (emailLabel = new Label("Your e-mail address:"));
p.add (emailField = new TextField(60));
p.add (orgLabel = new Label("Your orgainization:"));
p.add (orgField = new TextField(60));
p.add (msgBodyLabel = new Label("Your Message:"));
add (p);
add (msgBodyArea = new TextArea(3, 60));
// Set the size of the form
setSize (550, 500);
}
/**
* Return the value in the e-mail address field in the form
*/
public String email() {
return emailField.getText();
}
//
// Updated to the new Event model.
//
public void actionPerformed (ActionEvent e) {
if(e.getSource() == sendButton) {
//
// User clicked the Send button; send the message
//
try { applet.send(); }
catch (Exception ex) {
applet.showStatus("Error; message send failed:\n " +
ex.toString());
return;
}
applet.showStatus("Message sent");
return;
}
}
/**
* Return the contents of the body of the form, including any "hidden"
fields.
*/
public String message() {
String m = "";
m += nameLabel.getText();
m += nameField.getText();
m += "\n";
m += orgLabel.getText();
m += orgField.getText();
m += "\n";
m += "Web Origin:";
if (!applet.standalone) m += applet.getDocumentBase();
m += "\n";
m += "Date Sent:";
m += (new Date()).toString();
m += "\n";
m += msgBodyLabel.getText();
m += msgBodyArea.getText();
m += "\n";
return m;
}
} // end Form class
|