import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;


public class SendMail extends Applet
{
 TextField from 	= new TextField();
 TextField to 		= new TextField();
 TextField subject 	= new TextField();
 TextArea text		= new TextArea();
 Button send		= new Button("Send");
 Button reset		= new Button("Reset");
 Checkbox edit 		= new Checkbox("Edit");
 Font font;
 String to_string	= new String();
 String from_string	= new String();
 String subject_string	= new String();
 String text_string	= new String();

 Socket sock;
 PrintStream ps;

 String send_to, send_subject;

 public void init()
 {
  send_to = getParameter("send_to");
  send_subject = getParameter("send_subject");

  setLayout(null);
  font = new Font("Arial", 3, 14);

  to.reshape(100, 15, 180, 20);
  add(to);
  to.setText(send_to);
  
  from.reshape(100, 50, 180, 20);
  add(from);

  subject.reshape(100, 85, 180, 20);
  add(subject);
  subject.setText(send_subject);

  text.reshape(20, 120, 260, 125);
  add(text);
  
  edit.reshape(20, 260, 50, 10);
  add(edit);
  edit.setState(true);

  send.reshape(85, 260, 63, 25);
  add(send);
  send.setFont(font);
  send.setBackground(Color.green);
  send.setForeground(Color.white); 

  reset.reshape(163, 260, 63, 25);
  add(reset);
  reset.setFont(font);
  reset.setBackground(Color.red);  
  reset.setForeground(Color.white);

  
 }

  public void paint(Graphics g)
  {
   g.setFont(font);
   g.setColor(Color.blue);
   g.drawString("To:", 20, 30);
   g.drawString("From:", 20, 65);
   g.drawString("Subject:", 20, 100);
  }

  public boolean action(Event evt, Object obj)
  {
   if(evt.target instanceof Checkbox)
   {
    if(edit.getState()) //jos päällä
    {
     text.setEditable(true);
     from.setEditable(true);
     to.setEditable(true);
     reset.enable();
    }
    else {
          text.setEditable(false);
          from.setEditable(false);
          to.setEditable(false);
          reset.disable();
         }
   }
   if(evt.target instanceof Button)
   {
    if("Reset".equals(obj)) //reset nappi
    {
    text.setText("");
    repaint();
    }
   
    if("Send".equals(obj)) //send nappi
    {
     to_string = to.getText();
     from_string = from.getText();
     
     subject_string = subject.getText();
     text_string = text.getText();
     
     to_string = to_string.trim();
     from_string = from_string.trim();
     subject_string = subject_string.trim();

     if (check(to_string) && check(from_string))
     sendMail();
    }
   
  }
 return true;
 
}

public boolean check(String str)  
{

if ((str.indexOf("@") > 1) &&
     (str.indexOf("@") < str.length() - 1) &&
     (str.indexOf("@") > 0) &&
     (str.indexOf("@") == str.lastIndexOf("@")))
      {  showStatus("Thanks!");
	return true; 
      }

else  {
	showStatus("Invalid email address");
        to.setText(""); from.setText("");
	repaint();
	return false;
      }

}

  public void sendMail()  
{

	connectserver();
	writeserver();
	disconnect();
}


public void connectserver()  
{
	try {
	String hostname = getCodeBase().getHost();
        sock = new Socket(hostname, 25); // 25 portti num. SMTP
	ps = new PrintStream(sock.getOutputStream());
	    }  catch (Exception e)  {}
}	

public void writeserver()  {

	try  {
	InetAddress address = InetAddress.getLocalHost();
	ps.println("HELO" + address.getHostName());
	ps.println("MAIL FROM:" + from_string);
	ps.println("RCPT TO:" + to_string);
	ps.println("DATA");
	ps.println("To: " + to_string);
	ps.println("From: " + from_string);
	ps.println("Subject: " + subject_string);
	ps.println(text_string);
	ps.println(".");
	}  catch (Exception e)  {}
}

  public void disconnect()  
  {
   try { ps.println("QUIT"); }  catch (Exception e)  {}
   try { ps.close(); }  catch (Exception e)  {}	
	
  }
}    
