/*
 * Craig E. Ward
 * Kholoud Khateeb
 * CMSI 698 Internet Technologies
 * Spring 2003
 * Homework 3, Problem 2
 *
 * SimpleSmtpDemo.java
 *
 * A simple demostration of the SMTP protocol. Program is given an address,
 * assumed to be syntacically correct, and uses it as target of a message from
 * itself.
 */
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class SimpleSmtpDemo
{
    static private final int SMTP_PORT = 25;
    static private final String CRLF = "\r\n";
    static private boolean sentQuit = false;
    static private String smtpHost = "lion.lmu.edu";
    static private BufferedReader in;
    static private PrintWriter out;
 
    public static void main(String args[]) throws Exception
    {
        if (args.length != 1) {
            System.out.println("usage: java SimpleSmtpDemo user@domain");
            System.exit(1);
        }
        Socket socket = new Socket(smtpHost, SMTP_PORT);
        SimpleDateFormat sdf = 
            new SimpleDateFormat("EE, d MMM yyyy H:mm:ss");
        Date now = new Date();
        try {
            java.net.InetAddress me = socket.getLocalAddress();
            in = new BufferedReader(new 
                           InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            getResponse();
            sendLine("EHLO " + me.getHostName() +  CRLF);
            getResponse();
            sendLine("MAIL FROM:" + args[0] + CRLF);
            getResponse();
            sendLine("RCPT TO:" + args[0] + CRLF);
            getResponse();
            sendLine("DATA" + CRLF);
            getResponse();
            sendLine("From: " + args[0] + CRLF);
            sendLine("To: " + args[0] + CRLF);
            sendLine("Subject: Simple SMTP Demo" + CRLF);
            sendLine("Date: " + sdf.format(now) + " " +
                    zoneOffset(TimeZone.getDefault(), now) + CRLF + CRLF);
            sendLine("This is a simple example of mail sent through SMTP." 
                     + CRLF);
            sendLine(CRLF);
            sendLine("This is the last line." + CRLF);
            sendLine(CRLF + "." + CRLF);
            getResponse();
            sendQuit();
        } finally {
            socket.close();
        }
    }

    /*
     * Read the lines from the SMTP server until the continuation marker is not
     * present.
     */
    static private void getResponse() throws Exception
    {
        String serverResponse;
        do {
            serverResponse = in.readLine();
            System.out.println(serverResponse);
        } while (serverResponse.charAt(3) == '-');
	/*
	 * If the server doesn't like something, send a quit and exit.
	 */
        if (serverResponse.charAt(0) == '5') { 
            sendQuit();
            System.exit(1);
        }
    }

    /*
     * Send a command to the server
     */
    static private void sendLine(String text) throws Exception
    {
        out.write(text);
        out.flush();
        System.out.print(text);
    }

    /*
     * Send a quit and read the response.
     */
    static private void sendQuit() throws Exception
    {
        if (sentQuit == false) {
            sendLine("QUIT" + CRLF);
            sentQuit = true;
            getResponse();
        }
    }

    /*
     * Return new-style offset for time zone.
     */
    static private String zoneOffset(TimeZone timeZone, Date now)
    {
	/*
	 * If this were something other than a simple demo, we'd figure out the
	 * proper string for a time zone other than US Pacific.
	 */
        if (timeZone.inDaylightTime(now) == true)
            return "-0700";
        return "-0800";
    }
} // end class SimpleSmtpDemo
