/**
* Copyright (c)1996 Vishal Anand, All Rights Reserved.
*
* Description: Send E-mail (using SMTP) from within Java application,
* won't work with applets in WWW-browser
* (security violation !!!).
*
* Author's home page : http://www.geocities.com/SiliconValley/Park/3091
*
* Date : 24th Sept., 1996
*
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
*
**/
import java.io.*;
import java.net.*;
import java.lang.*;
import java.awt.*;
import java.applet.*;
public class SendMail extends Frame
{
// constructor
public SendMail()
{
super("Vishal's Send E-mail Utility");
MenuBar mbar = new MenuBar();
Menu m = new Menu("Send");
m.add(new MenuItem("Send e-mail"));
mbar.add(m);
setMenuBar(mbar);
pack();
resize(300,200);
show();
}
/**** main ****/
public static void main(String args[])
{
new SendMail();
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case Event.WINDOW_DESTROY:
System.exit(0);
return true;
default:
super.handleEvent(evt);
}
return false;
}
public boolean action(Event evt, Object arg)
{
if(evt.target instanceof MenuItem)
{
if("Send e-mail".equals(arg))
{
SendMailDialog smd = new SendMailDialog(this);
smd.show();
return true;
}
}
return super.handleEvent(evt);
}
}
/*************************************************************************/
/********************** Program for Send Mail Dialog Box *****************/
/*************************************************************************/
class SendMailDialog extends Dialog
{
TextField mailto, mailfrom, subject;
TextArea message;
Button ok, cancel;
Frame parent;
String msg;
//constructor
public SendMailDialog(Frame parent)
{
super(parent,"Send E-mail",true);
parent=parent;
setResizable(false);
setLayout(new FlowLayout());
Panel p1 = new Panel();
/* mail to: */
p1.add(new Label("E-mail To:"));
mailto = new TextField(50);
mailto.setBackground(Color.black);
mailto.setForeground(Color.green);
p1.add(mailto);
add(p1);
/* subject : */
Panel p2 = new Panel();
p2.add(new Label("Subject: "));
subject = new TextField(50);
subject.setBackground(Color.black);
subject.setForeground(Color.green);
p2.add(subject);
add(p2);
/* mail from: */
Panel p3 = new Panel();
p3.add(new Label("E-mail From:"));
mailfrom = new TextField(50);
mailfrom.setBackground(Color.black);
mailfrom.setForeground(Color.green);
p3.add(mailfrom);
add(p3);
/* message (actual message text) */
Panel p4 = new Panel();
p4.add(new Label("Enter Message:"));
message = new TextArea(8,60);
message.setBackground(Color.black);
message.setForeground(Color.red);
p4.add(message);
add(p4);
Panel p5 = new Panel();
p5.add(ok = new Button("send"));
p5.add(cancel = new Button("cancel"));
add(p5);
pack();
resize(700,310);
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case Event.WINDOW_DESTROY:
dispose();
return true;
default:
super.handleEvent(evt);
}
return false;
}
public boolean action(Event evt, Object arg)
{
if(evt.target instanceof Button)
{
if(arg.equals("send"))
{
if( (mailto.getText().indexOf("@") == -1) || (mailfrom.getText().indexOf("@") == -1) )
{
msg = "Either of the E-mail addresses is incorrect";
error error_dialog = new error(parent,msg);
error_dialog.show();
}
else
{
proceed_to_send_mail();
}
dispose();
return true;
}
if(arg.equals("cancel"))
{
dispose();
return true;
}
}
if(evt.target instanceof TextField)
{
if(evt.key == 10 || evt.key==9)
message.requestFocus();
return true;
}
if(evt.target instanceof TextArea)
{
if(evt.key == 10)
message.requestFocus();
return true;
}
return super.handleEvent(evt);
}
/********************************/
public void proceed_to_send_mail()
{
Socket socket;
InputStream in;
OutputStream out;
DataInputStream din;
PrintStream prout;
String incoming;
int SMTPport = 25;
String MailHost ;
/* Get the first occurence of '@' in the recepient's e-mail addr,
the string after that is supposed to be the recepient's server name
(viz the DNS name)
*/
int index = ( mailto.getText() ).indexOf("@");
MailHost = new String( mailto.getText().substring(index+1) );
String HELO = "HELO";
String MAILFROM = "MAIL FROM:";
MAILFROM = MAILFROM + "<" + mailfrom.getText() + ">" ;
String RCPTTO = "RCPT TO:";
RCPTTO = RCPTTO + "<" + mailto.getText() + ">" ;
String DATA = "DATA";
String SUBJECT = "Subject:";
SUBJECT = SUBJECT + subject.getText();
String msg; // for printing error messages
/* connect to the mail server */
try
{
socket = new Socket(MailHost, SMTPport);
} catch (IOException e)
{
msg = "Could not connect to the destination";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
try
{
in = socket.getInputStream();
din = new DataInputStream(in);
out = socket.getOutputStream();
prout = new PrintStream(out);
}
catch (IOException e)
{
msg = "Error opening inputstream.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* OK, we're connected, let's be friendly and say hello to the mail server... */
prout.println(HELO);
prout.flush();
try
{
incoming = din.readLine();
}
catch (IOException e)
{
msg = "Error reading from socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* let server know YOU wanna send mail... */
prout.println(MAILFROM);
prout.flush();
try
{
incoming = din.readLine();
}
catch (IOException e)
{
msg = "Error reading from socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* let server know WHOM you're gonna send mail to... */
prout.println(RCPTTO);
prout.flush();
try
{
incoming = din.readLine();
}
catch (IOException e)
{
msg = "Error reading from socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* let server know you're now gonna send the message contents... */
prout.println(DATA);
prout.flush();
try
{
incoming = din.readLine();
}
catch (IOException e)
{
msg = "Error reading from socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* send the subject...
As per SMTP specifications, u have to send a blank line after the
subject, and then begin the actual message text
*/
SUBJECT = SUBJECT + "\r\n\r\n";
prout.println(SUBJECT);
prout.flush();
/* finally send the the actual message...
you must terminate your message string with
"\r\n.\r\n" to indicate end of message.
i.e. according SMTP protocol, a '.' on a new line is considered as
the end of message
*/
message.appendText("\r\n.\r\n");
prout.println(message.getText());
prout.flush();
try
{
incoming = din.readLine();
}
catch (IOException e)
{
msg = "Error reading from socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
/* we're done, disconnect from server */
try
{
socket.close();
}
catch (IOException e)
{
msg = "Error closing socket.";
error error_dialog = new error(parent,msg);
error_dialog.show();
return;
}
} // end of function proceed_to_send_mail()
} // end of class SendMailDialog
/*************************************************************************/
/********************** Program for Error Msg Dialog Box *****************/
/*************************************************************************/
class error extends Dialog
{
String error_message;
// constructor
public error(Frame parent,String msg)
{
super(parent,"ERROR MESSAGE !!!",true);
setResizable(false);
setBackground(Color.red);
error_message = msg;
resize(500,200);
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case Event.WINDOW_DESTROY:
dispose();
return true;
default:
super.handleEvent(evt);
}
return false;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString("ERROR:",50,50);
g.drawString(error_message,20,75);
}
} //end of class error
               (
geocities.com/siliconvalley/park/Park)                   (
geocities.com/siliconvalley/park)                   (
geocities.com/siliconvalley)