import java.net.*;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SendBillMail
{
 public static void main(String s[])
 {
// *************************************************************************
//  Send fake mail from Bill Gates
//
//  SendBillMail <mail server> <recipient address>
//
//  ex.  SendBillMail mail.company.com gday@gday.gdy
// *************************************************************************

   SendBillMail t = new SendBillMail();
   t.sendMail(s[0], s[1]);

  }

 public void sendMail(String mailServer, String recipient)
 {
 String mailbody = "";
 FileInputStream infile = null;
 int c;
   try
   {
      Socket s = new Socket(mailServer, 25);
      BufferedReader in = new BufferedReader
          (new InputStreamReader(s.getInputStream(), "8859_1"));
      BufferedWriter out = new BufferedWriter
          (new OutputStreamWriter(s.getOutputStream(), "8859_1"));

      send(in, out, "HELO theWorld");
// warning : some mail server validate the sender address
//           in the MAIL FROm command, put your real address here
      send(in, out, "MAIL FROM: <christiant@look.com.au>");
      send(in, out, "RCPT TO: " + recipient);
      send(in, out, "DATA");
      send(out, "Subject: Message from the borg King");
      send(out, "From: Bill Gates <Uncle.Bill@Microshaft.coms>");
      send (out, "\n");
// message body
      infile = new FileInputStream("mailbody.txt");
      while ((c = infile.read()) != -1)
        {
                mailbody = mailbody + (char)c;
        }
      send(out, mailbody);
//      System.out.println("1");
      send(out, "www.yeahgday.com");
//      System.out.println("2");
      send(out, "\n.\n");
//      System.out.println("3");
      send(in, out, "QUIT");
//      System.out.println("4");
      s.close();
//      System.out.println("5");
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 }

 public void send(BufferedReader in, BufferedWriter out, String s)
 {
   try
   {
      out.write(s + "\n");
      out.flush();
      System.out.println(s);
      s = in.readLine();
      System.out.println(s);
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 }

 public void send(BufferedWriter out, String s)
 {
   try
   {
      out.write(s + "\n");
      out.flush();
      System.out.println(s);
   }
   catch (Exception e)
   {
      e.printStackTrace();
   }
 }
}
