BurgerBob 0 Newbie Poster

Hi guys,

I'm taking a Networking & Internet Systems module... have little experience working with java and am trying to work through an assignment... hopefully you can help!

I've been given skeleton code to complete for a program that acts as a simple email client that uses TCP/IP sockets to interact with an SMTP server to send email messages.

It seems fairly straight forward, but when i send the DATA command... program seems to hang... can't figure out why... any ideas?

Thanks

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

public class smtpClient {    
    
    // Put in a loop with the RSET - RESET command?
    // Can i QUIT/close connection at any time?
    
    public static void main(String[] args) throws Exception{
        
        // Create TCP connection to mail server (e.g. mail1.eircom.net) on TCP port 25
        System.out.println("Enter the mail server you wish to connect to: ");
        String host = new String();
        Scanner scanUser = new Scanner(System.in);
        host = scanUser.next();
        String port = new String();
        System.out.print("Enter port#: ");
        port = scanUser.next();
        int portNumber = Integer.parseInt(port);        
        Socket socket = new Socket(host, portNumber);  
      
        // InputStream
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        // OutputStream        
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        
        // Read greeting from the server
        System.out.println(" "); 
        String response = in.readLine();         
        if (!response.startsWith("220"))  
            throw new Exception("No reply from server\n");         
        else 
            System.out.println(response + "\n");        
       
        // Send HELO command to initiate SMTP conversation
        // + specify domain to identify self - get server response 
        System.out.println("Enter name of your mail domain (e.g. eircom.net):");
        String heloDomain = scanUser.next();
        System.out.println(" "); 
        String fullHeloCommand =  "HELO " + heloDomain + "\r\n";
        System.out.print(fullHeloCommand); 
        out.write(fullHeloCommand.getBytes()); 
        response = in.readLine();         
        if (!response.startsWith("250"))  
            throw new Exception("No reply from server\n"); 
        else 
            System.out.println(response + "\n"); 
        
        // Send MAIL FROM command (to specify who the mail is from)
        System.out.println("Enter your email address (e.g. me@eircom.net): ");
        String myEmail = new String();
        myEmail = scanUser.next();        
        String mailFrom = new String();
        mailFrom = "MAIL FROM:  <" + myEmail + ">\r\n";
        System.out.println(" ");
        System.out.print(mailFrom);
        out.write(mailFrom.getBytes());
        response = in.readLine();        
        if (!response.startsWith("250")) 
            throw new Exception("No reply from server\n");
        else 
            System.out.println(response);
       
        // Send RCPT TO command (to indicate the recipient of the email being sent)   
        System.out.println("\nEnter recipient's email address (e.g. someone.else@eircom.net): ");
        String recipientEmail = new String();
        recipientEmail = scanUser.next();
        String rcptTo = new String();
        rcptTo = "RCPT TO:  <" + recipientEmail + ">\r\n";
        System.out.println(" ");
        System.out.print(rcptTo);
        out.write(rcptTo.getBytes());
        response = in.readLine();        
        if(!response.startsWith("250")) 
            throw new Exception("No reply from server\n");
        else 
            System.out.println(response + "\n");
        
        // Send DATA command to signify that a stream of data, 
        // ie. email message body, will follow
        String data = new String();
        data = "DATA";
        System.out.println(data);
        out.write(data.getBytes());
        response = in.readLine();        
        if(!response.startsWith("354")) 
            throw new Exception("No reply from server\n");
        else 
            System.out.println(response);       
        
        // Send message data 
        System.out.println("Enter your message, enter '.' on a separate line to end message data entry:\n");
        String inFromUser = new String();
        while(inFromUser.charAt(0) != '.')
        {
            inFromUser = scanUser.next();
            out.write(inFromUser.getBytes());
        }
        
        // Terminate data stream by a single "." (period) on a line by itself
        out.write(inFromUser.getBytes());
        response = in.readLine();        
        if(!response.startsWith("250")) 
            throw new Exception("No reply from server\n");
        else 
            System.out.println(response);
         
        //  terminate SMTP connection
        String quit = new String();
        quit = "QUIT";
        out.write(quit.getBytes());
    }       
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.