import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Test {

    private static void check(String reply) throws Exception {
        // line starting with 2XX or 3XX messages are afirmative
        if (reply.startsWith("2")||reply.startsWith("3")) return;
        throw new Exception(reply);
        // the error message propagates through an exception
    }

    public static void SendMail(String myComputer,  String smtpServer, 
                                String sendAddr,    String sendName,
                                String rcptAddr,    String rcptName,
                                String emSubject,   String emBody ) {
        // create a connection using a socket
        Socket          sock=null;
        BufferedReader  SR=null;
        PrintWriter     SW=null;

        try {
            // create a connection using a socket
            sock = new Socket(smtpServer, 25); // SMTP is on port 25
            // create a stream for reading via socket connection
            SR=new BufferedReader(new InputStreamReader(sock.getInputStream()));
            // create a stream for writing via socket connection
            SW=new PrintWriter (sock.getOutputStream());
            // PrintWriter may not flush the buffer automatically when println() is called
        } catch (Exception e) {
            System.out.println("ERROR while connecting to the server: "+e);
            return; // exit the function early
        }
    
        
        try {
            String feedback;
    
            SW.println("helo "+myComputer);
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);
    
            SW.println("mail from: "+sendAddr);
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);
    
            SW.println("rcpt to: " + rcptAddr );
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);
    
            SW.println("data");
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);
    
            SW.println("From: "     +sendName+" <"+sendAddr+">");
            SW.println("To: "       +rcptName+" <"+rcptAddr+">");
            SW.println("Subject: "  +emSubject);
            SW.println();
            SW.println(emBody);
            SW.println(".");
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);
    
            SW.println("quit");
            SW.flush();
            feedback=SR.readLine();
            System.out.println(feedback);
            check(feedback);

            System.out.println("Email sent.");
        
        } catch (Exception e) {
            System.out.println("ERROR while sending email: "+e);
            return; // exit the function, finally will be executed though
        } finally {
            try {
                sock.close();
                // we do have to close the socket no matter what
            } catch (Exception e2) {
                // do nothing if error closing
            }
            
        }

    }


    public static void main(String args[]) {
        SendMail(   "localhost.bradley.edu",            // your computer name   // replace with yoru computer nameor IP address
                    "cegt201.bradley.edu",              // SMTP server name     // replace with an SMTP server local to your domain
                    "ee-web at bradley dot edu",        // the sender addr      // replace text inside ""s with real address
                    "EE-WEB Student",                   // the sender name
                    "olekmali at bradley dot edu",      // the recipient addr   // replace text inside ""s with real address
                    "EE-WEB Instructor",                // the recipient name
                    "SMTP client in Java test",         // the subject of the message
                    "This is the message\n that my Java program sent."
                );                                      // the body of the message

    }
}