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

public class smtpClient {
    public static void main(String[] args) {

        Socket smtpSocket = null;  
        DataOutputStream os = null;// os: output stream
        DataInputStream is = null;// is: input stream

	// Initialization section:
	// Try to open a socket on port 25
	// Try to open input and output streams
        try {
            smtpSocket = new Socket("hub0.engr.sc.edu", 25);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: hostname");
        }
    if (smtpSocket != null && os != null && is != null) {
            try {
		// The capital string before each colon has a special meaning to SMTP
		// you may want to read the SMTP specification, RFC1822/3
        	os.writeBytes("HELO\r\n");    

                os.writeBytes("MAIL From:testsmpt@env.edu\r\n");

                os.writeBytes("RCPT To: @cse.sc.edu\r\n");

                os.writeBytes("DATA\r\n");

                os.writeBytes("From: test@header.com\r\n");
                os.writeBytes("Subject: testing\r\n");
                os.writeBytes("\r\nHi this is a testing message\r\nBye\r\n"); // message body
                os.writeBytes("\r\n.\r\n");

        	os.writeBytes("QUIT\r\n");
		// keep on reading from/to the socket till we receive the "Ok" from SMTP,
                String responseLine;
                while ((responseLine = is.readLine()) != null) {
                    System.out.println("Server: " + responseLine);
                    if (responseLine.indexOf("Ok") != -1) {
                      break;
                    }
                }
// clean up:
// close the output stream
// close the input stream
// close the socket
				os.close();
                is.close();
                smtpSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
}



