import MessageException;
import FolderException;
import LoginException;
import NetworkConnection;
import SimpleOutput;
import MessageHeader;
import MailMessage;
import SimpleInput;

/**
 * A protocal for Java to interact with a SMTP server
 *
 * @author Ben Kaiser
 * @author Josh Vickery
 * @author Peter van der Linden
 * @author Dan Hawando
 * @version .01 of October 1999
 *
 * @modified from version 1.1 to become a class instead of an interface
 * @modified the interface was renamed MailboxInterface.java
 * @modified actual implimentation
 */

public class SendMail { 
    

    //+--------+
    //| Fields |
    //+--------+
    
    /**
     * Create a new Socket object
     */
    java.net.Socket sock;
    
    /**
     * Create a new data input stream to read from the server
     */
    java.io.BufferedReader br;
    
    /**
     * Create a new stream printer to print to the server
     */
    java.io.PrintWriter ps; 

    SimpleOutput out = new SimpleOutput();

    SimpleInput in = new SimpleInput();
    

    public SendMail() 
	throws ConnectionException, java.io.IOException
    {
           try { 
	       // open the socket
        sock = new java.net.Socket("pioneerserver.grinnell.edu", 25);
           } // try
           catch (Exception e1) {
               throw new ConnectionException("server unreachable");
	   } //catch
           try {
	       // prepare for input
               br = 
                   new 
                   java.io.BufferedReader(new java.io.InputStreamReader(sock.getInputStream()));
           } //try
           catch (Exception e2) {
               throw new ConnectionException("unable to establish connection");
           } //catch
           try {
	       // prepare for output
        ps = new java.io.PrintWriter(sock.getOutputStream(), true);
           } //try
           catch (Exception e3) {
               throw new ConnectionException("unable to connect");
           } //catch
	   try {
	       // read the greating from the SMTP server
	       out.println( br.readLine() );
	   } //try
	   catch (Exception ex) {}
	   
    } // Mailbox()
    
      
    public void bye() 
    throws java.io.IOException {
	SimpleOutput out = new SimpleOutput();
	// logout
        ps.println("quit");
	sock.close();
    } //bye()
   
    public void send(String from, String to, String message) 
    throws java.io.IOException {
	ps.print("mail from: " + from);
	ps.println('\r');
	out.println(br.readLine());
	ps.print("rcpt to: " + to);
	ps.println('\r');
	out.println(br.readLine());
	ps.print("data");
	ps.println('\r');
	out.println(br.readLine());
	ps.print(message);
	ps.println('\r');
	ps.println(".");
	ps.println('\r');
	out.println(br.readLine());
     }
}
	
    

