package net;
import ip.gui.frames.ClosableFrame;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
public class SendMailApplet extends Applet {
private String recipientEmailAddress = null;
private String emailServerHostName = null;
private boolean standalone = false;
private int smtpPort = 25;
private Socket socket;
private PrintWriter pw;
private InetAddress remoteInternetAddress;
private InetAddress localInternetAddress;
private EmailClientGui form;
public void init() {
setBackground(Color.white);
form = new EmailClientGui(this);
add(form);
setSize(600, 450);
if (emailServerHostName == null)
emailServerHostName = getCodeBase().getHost();
if (recipientEmailAddress == null)
recipientEmailAddress = getParameter("RECIPIENT");
}
public void showStatus(String s) {
System.out.println(s);
if (standalone) return;
super.showStatus(s);
}
private BufferedReader br;
public void send() throws IOException, Exception {
socket = new Socket(emailServerHostName, smtpPort);
try {
remoteInternetAddress =
socket.getInetAddress();
localInternetAddress =
remoteInternetAddress.getLocalHost();
pw = new PrintWriter(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sendline("HELO " + localInternetAddress.toString());
sendline("MAIL FROM:" + form.email());
sendline("RCPT TO:" + recipientEmailAddress);
sendline("DATA");
sendline(form.message());
sendline(".");
} catch (Exception ex) {
socket.close();
throw ex;
}
socket.close();
}
void sendline(String data) throws IOException {
System.out.println("sendline out:" + data);
pw.println(data);
pw.flush();
String s = br.readLine();
System.out.println("sendline in:" + s);
}
public static void main(String args[]) {
SendMailApplet ap = new SendMailApplet();
ap.emailServerHostName = "192.168.1.96";
ap.recipientEmailAddress = "lyon@docjava.com";
ap.standalone = true;
ClosableFrame fr = new ClosableFrame("SendMail");
ap.init();
fr.add("Center", ap);
fr.setSize(600, 450);
fr.show();
ap.start();
}
public String getWebmasterEmail() {
return recipientEmailAddress;
}
public void setWebmasterEmail(String webmasterEmail) {
this.recipientEmailAddress = webmasterEmail;
}
public String getServerHostName() {
return emailServerHostName;
}
public void setServerHostName(String serverHostName) {
this.emailServerHostName = serverHostName;
}
public boolean isStandalone() {
return standalone;
}
public void setStandalone(boolean standalone) {
this.standalone = standalone;
}
public int getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(int smtpPort) {
this.smtpPort = smtpPort;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public PrintWriter getPs() {
return pw;
}
public void setPs(PrintWriter ps) {
this.pw = ps;
}
public InetAddress getRina() {
return remoteInternetAddress;
}
public void setRina(InetAddress rina) {
this.remoteInternetAddress = rina;
}
public InetAddress getLina() {
return localInternetAddress;
}
public void setLina(InetAddress lina) {
this.localInternetAddress = lina;
}
public EmailClientGui getForm() {
return form;
}
public void setForm(EmailClientGui form) {
this.form = form;
}
public BufferedReader getBuf_reader() {
return br;
}
public void setBuf_reader(BufferedReader buf_reader) {
this.br = buf_reader;
}
}
class EmailClientGui extends Panel implements ActionListener {
SendMailApplet applet;
Label nameLabel;
TextField nameField;
Label emailLabel;
TextField emailField;
Label orgLabel;
TextField orgField;
Label msgBodyLabel;
TextArea msgBodyArea;
Button sendButton;
public EmailClientGui(SendMailApplet ap) {
applet = ap;
setBackground(Color.white);
setLayout(new GridLayout(2, 1));
Panel p = new Panel();
p.setLayout(new GridLayout(8, 1));
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 addBk.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));
setSize(550, 500);
}
public String email() {
return emailField.getText();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
try {
applet.send();
} catch (Exception ex) {
applet.showStatus("Error; message send failed:\n " + ex.toString());
return;
}
applet.showStatus("Message sent");
return;
}
}
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.isStandalone()) m += applet.getDocumentBase();
m += "\n";
m += "Date Sent:";
m += (new Date()).toString();
m += "\n";
m += msgBodyLabel.getText();
m += msgBodyArea.getText();
m += "\n";
return m;
}
}