CST1642 Advanced Java II
Lab 3 notes


The SMTP (Simple Mail Transfer Protocol) server set up on kaboom.ridgewater.net.

Remember, this is outgoing email only. POP3 (Post Office Protocol) is not set up on this server, so you will not be able to receive mail to this account.

Here is the information you need. You will not need all of it, but I'll supply everything.

SMTP server: kaboom.ridgewater.net
IP address of kaboom.ridgewater.net: 134.29.175.68
SMTP service is running on port: 25

Server requires authentication to send mail for you.
Your username (accountname) is your last name followed by the first three characters of your first name all in lower case.
Example: John Doe would be doejoh
Password is your student ID number. All eight digits.


To figure out the command sequence for authentication, I set up Outlook Express first and turned logging on.  Then I sent some email.  Doing this also verified that the SMTP server actually works before I start coding Java.  Here is the actual log data that was captured.  The only thing changed in the log was I changed the actually encrypted login and username string so that hackers cannot use this log to break into the server.

Outlook Express 6.00.2800.1106 (xpsp1.020828-1920)
SMTP Log started at 01/27/2003 11:33:50
SMTP: 11:33:51 [rx] 220 ridgewater.net Microsoft ESMTP MAIL Service, Version: 5.0.2195.5329 ready at Mon, 27 Jan 2003 11:30:24 -0600
SMTP: 11:33:51 [tx] EHLO ABenusa
SMTP: 11:33:51 [rx] 250-ridgewater.net Hello [12.218.16.58]
SMTP: 11:33:51 [rx] 250-AUTH=LOGIN
SMTP: 11:33:51 [rx] 250-AUTH LOGIN
SMTP: 11:33:51 [rx] 250-TURN
SMTP: 11:33:51 [rx] 250-ATRN
SMTP: 11:33:51 [rx] 250-SIZE 2097152
SMTP: 11:33:51 [rx] 250-ETRN
SMTP: 11:33:51 [rx] 250-PIPELINING
SMTP: 11:33:51 [rx] 250-DSN
SMTP: 11:33:51 [rx] 250-ENHANCEDSTATUSCODES
SMTP: 11:33:51 [rx] 250-8bitmime
SMTP: 11:33:51 [rx] 250-BINARYMIME
SMTP: 11:33:51 [rx] 250-CHUNKING
SMTP: 11:33:51 [rx] 250-VRFY
SMTP: 11:33:51 [rx] 250 OK
SMTP: 11:33:51 [tx] AUTH LOGIN
SMTP: 11:33:51 [rx] 334 VXNlcm5hbWU6
SMTP: 11:33:51 [tx] ABCdefGHIjkl (Changed for security reasons)
SMTP: 11:33:51 [rx] 334 UGFzc3dvcmQ6
SMTP: 11:33:51 [tx] MNOpqrSTUvwx (Changed for security reasons)
SMTP: 11:33:51 [rx] 235 2.7.0 Authentication successful
SMTP: 11:33:51 [tx] MAIL FROM: <benusaall@kaboom.ridgewater.net>
SMTP: 11:33:51 [rx] 250 2.1.0 benusaall@kaboom.ridgewater.net....Sender OK
SMTP: 11:33:51 [tx] RCPT TO: <alben1234@hotmail.com>
SMTP: 11:33:51 [rx] 250 2.1.5 alben1234@hotmail.com
SMTP: 11:33:51 [tx] DATA
SMTP: 11:33:51 [rx] 354 Start mail input; end with <CRLF>.<CRLF>
SMTP: 11:33:51 [tx]
.
SMTP: 11:33:52 [rx] 250 2.6.0 <001201c2c62a$4217d760$6401a8c0@ABenusa> Queued mail for delivery
SMTP: 11:33:52 [tx] QUIT
SMTP: 11:33:52 [rx] 221 2.0.0 ridgewater.net Service closing transmission channel


Here is the actual log data when communicating between my Java application and the SMTP mail server.

220 ridgewater.net Microsoft ESMTP MAIL Service, Version: 5.0.2195.5329 ready at Mon, 27 Jan 2003 11:27:16 -0600
EHLO ABenusa
250-ridgewater.net Hello [12.218.16.58]
AUTH LOGIN
250-AUTH=LOGIN
250-AUTH LOGIN
250-TURN
250-ATRN
250-SIZE 2097152
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250 OK
334 VXNlcm5hbWU6
ABCdefGHIjkl (Changed for security reasons)
334 UGFzc3dvcmQ6
MNOpqrSTUvwx (Changed for security reasons)
235 2.7.0 Authentication successful
MAIL FROM: <benusaall@kaboom.ridgewater.net>
250 2.1.0 benusaall@kaboom.ridgewater.net....Sender OK
RCPT TO: <alben1234@hotmail.com>
250 2.1.5 alben1234@hotmail.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Test message.
.
250 2.6.0 <KABOOMWtIIZS2TRqf6900000006@ridgewater.net> Queued mail for delivery
QUIT
221 2.0.0 ridgewater.net Service closing transmission channel


The username and password to your account has to be encrypted using Base64.  Below is the code for doing that.

 // Base64 Encoding routine  
   private final static char base64Array [] = {
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
      'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
      'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
      'w', 'x', 'y', 'z', '0', '1', '2', '3',
      '4', '5', '6', '7', '8', '9', '+', '/'
 };

  private static String base64Encode (String string)    {
    String encodedString = "";
    byte bytes [] = string.getBytes ();
    int i = 0;
    int pad = 0;
    while (i < bytes.length) {
      byte b1 = bytes [i++];
      byte b2;
      byte b3;
      if (i >= bytes.length) {
         b2 = 0;
         b3 = 0;
         pad = 2;
         }
      else {
         b2 = bytes [i++];
         if (i >= bytes.length) {
            b3 = 0;
            pad = 1;
            }
         else
            b3 = bytes [i++];
         }
      byte c1 = (byte)(b1 >> 2);
      byte c2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
      byte c3 = (byte)(((b2 & 0xf) << 2) | (b3 >> 6));
      byte c4 = (byte)(b3 & 0x3f);
      encodedString += base64Array [c1];
      encodedString += base64Array [c2];
      switch (pad) {
       case 0:
         encodedString += base64Array [c3];
         encodedString += base64Array [c4];
         break;
       case 1:
         encodedString += base64Array [c3];
         encodedString += "=";
         break;
       case 2:
         encodedString += "==";
         break;
       }
      }
      return encodedString;
  }
  

Here is the heart of the program:

         // Connect to the SMTP server on the standard port of 25
         java.net.Socket s = new java.net.Socket("kaboom.ridgewater.net", 25);

         out = new java.io.PrintWriter(s.getOutputStream());
         in = new java.io.BufferedReader(new
            java.io.InputStreamReader(s.getInputStream()));

         String hostName
            = java.net.InetAddress.getLocalHost().getHostName();

         receive();
         // We are doing authentication, so it is "EHLO" instead of "HELO"
         send("EHLO " + hostName);
         receive();
         // Login authentication
         send("AUTH LOGIN");
         // Read several lines that the server sends back to us.  We don't care what it is,
         // but we need to accept it.
         receive(); receive(); receive(); receive(); receive(); receive(); receive(); receive();
         receive(); receive(); receive(); receive(); receive(); receive(); receive();
         // Send the encoded username (which is last name followed by 1st 3 char of first name).
         send(base64Encode("benusaall")); //username
         receive();
         // Send the encoded password (which is the student ID)
         send(base64Encode("00000000")); //password
         receive();
         // You should say who you are to the eventual recipient of the email.
         // \u0040 is unicode for "@".
         send("MAIL FROM: ");
         receive();
         // You need to say who your recipient is. \u0040 is unicode for "@".
         send("RCPT TO: <" + txtEmailAddr.getText() + ">");
         receive();
         // Start sending the actual text body of the email.
         send("DATA");
         receive();
         java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
            txtAreaMessage.getText(), "\n");
         while (tokenizer.hasMoreTokens())
            send(tokenizer.nextToken());
         // Signify this is the end of the email.
         send(".");
         receive();
         // Quit the connection
         send("QUIT");
         receive();
         s.close();

I would highly recommend displaying your communications between the Java applet and the server in a window for diagnostics purposes.