package server.sendmail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
public class SendMailApplet implements Runnable {
String m_sRecipientEmail = null; String m_sSenderEmail = null; String m_sServerHostName = null; String m_sMessage = null;
boolean m_bStandalone = false;
int smtpPort = 25;
Socket socket;
PrintStream ps;
BufferedReader dis;
InetAddress rina;
InetAddress lina;
Thread thread = new Thread(this);
public void emailLyon(String msg) {
email(msg,
"192.168.1.95",
"lyon@docjava.com",
"lyon@docjava.com");
}
public void run() {
sendNoException();
System.out.println("email was sent!");
}
public void email(String msg,
String m_sServerHostName,
String m_sRecipientEmail,
String m_sSenderEmail) {
setSenderEmail(m_sSenderEmail);
setRecipientEmail(m_sRecipientEmail);
setMailServerHostName(m_sServerHostName);
setMessage(msg);
thread.start();
}
public void sendNoException() {
try {
send();
} catch (IOException e) {
e.printStackTrace();
}
}
public void send() throws IOException {
socket = new Socket(m_sServerHostName, smtpPort);
try {
rina = socket.getInetAddress();
lina = rina.getLocalHost();
ps = new PrintStream(socket.getOutputStream());
dis = new BufferedReader(
new InputStreamReader((socket.getInputStream())));
sendline("HELO " + lina.toString());
sendline("MAIL FROM:" + m_sSenderEmail);
sendline("RCPT TO:" + m_sRecipientEmail);
sendline("DATA");
sendline(m_sMessage);
sendline(".");
} catch (Exception ex) {
socket.close();
System.out.println("Error; message send failed:\n " + ex.getMessage());
}
socket.close();
}
void sendline(String data) throws IOException {
ps.println(data);
ps.flush();
String s = dis.readLine();
System.out.println("sendline in:" + s);
}
public void setMessage(String msg) {
this.m_sMessage = msg;
}
public void setSenderEmail(String email) {
this.m_sSenderEmail = email;
}
public void setRecipientEmail(String email) {
this.m_sRecipientEmail = email;
}
public static void main(String args[]) {
SendMailApplet sma = new SendMailApplet();
sma.emailLyon("This is a test!!");
System.out.println("Thread was started to send an email");
}
public void setMailServerHostName(String host) {
this.m_sServerHostName = host;
}
}