import java.io.*;
import java.net.*;

public class SMTPDemo {

  public static void main(String args[])
      throws IOException, UnknownHostException {
    String msgFile;
    String from, to, mailHost;

    if (args.length != 4) {
      System.out.println ("Usage: java SMTPDemo msgFile from to mailHost");
      System.exit (10);
    }

    msgFile   = args[0];
    from      = args[1];
    to        = args[2];
    mailHost  = args[3];

    checkEmailAddress (from);
    checkEmailAddress (to);

    SMTP mail = new SMTP(mailHost);
    if (mail != null) {
      if (mail.send (new FileReader(msgFile), from, to) ) {
        System.out.println ("Mail sent.");
      } else {
        System.out.println ("Connect to SMTP server failed!");
      }
    }
    System.out.println("Done.");
  }

  static void checkEmailAddress (String address) {
    if (address.indexOf('@') == -1) {
      System.out.println ("Invalid e-mail address '" + address + "'");
      System.exit (10);
    }
  }
}

//-------------------------------------------------------------------
class SMTP {

  public final static int SMTP_PORT = 25;

  InetAddress mailHost;
  InetAddress ourselves;
  BufferedReader in;
  PrintWriter out;

  public SMTP (String host) throws UnknownHostException {

    mailHost = InetAddress.getByName(host);
    ourselves= InetAddress.getLocalHost();

    System.out.println ("mailhost = " + mailHost);
    System.out.println ("localhost= " + ourselves);
    System.out.println ("SMTP constructor done\n");
  }

  public boolean send (FileReader msgg, String from, String to) throws IOException {
    Socket smtpPipe;
    InputStream inn;
    OutputStream outt;
    BufferedReader msg;

    msg = new BufferedReader (msgg);

    smtpPipe = new Socket (mailHost, SMTP_PORT);
    if (smtpPipe == null) {
      return false;
    }

    // get raw streams
    inn  = smtpPipe.getInputStream();
    outt = smtpPipe.getOutputStream();

    // turn into usable ones
    in   = new BufferedReader (new InputStreamReader (inn));
    out = new PrintWriter (new OutputStreamWriter (outt), true);

    if (inn==null || outt==null) {
      System.out.println ("Failed to open streams to socket.");
      return false;
    }

    String initialID = in.readLine();
    System.out.println (initialID);

    System.out.println ("HELO " + ourselves.getHostName());
           out.println ("HELO " + ourselves.getHostName());

    String welcome = in.readLine(); 
    System.out.println (welcome);

    System.out.println ("MAIL From:<" + from + ">");
           out.println ("MAIL From:<" + from + ">");

    String senderOK = in.readLine(); 
    System.out.println (senderOK);

    System.out.println ("RCPT TO:<" + to + ">");
           out.println ("RCPT TO:<" + to + ">");

    String recipientOK = in.readLine(); 
    System.out.println (recipientOK);

    System.out.println ("DATA");
           out.println ("DATA");

    String line;
    while ((line = msg.readLine()) != null) {
      out.println(line);
    }
    System.out.println (".");
           out.println (".");

    String acceptedOK = in.readLine();
    System.out.println (acceptedOK);

    System.out.println ("QUIT");
           out.println ("QUIT");

    return true;
  }
}
