Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMTP Mail Client

Status
Not open for further replies.

nikki02

Programmer
Feb 3, 2008
1
US
I'm trying to figure this out with out any Java experience. I'm trying to write a program that will send a SMTP email. I was given a shell and I do know how to establish a connection. My next question is how do I Send MAIL FROM command, Send RCPT TO command, Send DATA command, Send message data and End with line with a single period. Will this be just a matter or printing this information? Someone please help me figure this out.

Code:
import java.io.*;
import java.net.*;

public class EmailSender
{
   public static void main(String[] args) throws Exception
   {
      // Establish a TCP connection with the mail server.
      
	Socket TCPConnection = new TCPConnection("mail.bellsouth.net, 25");

      // Create a BufferedReader to read a line at a time.

      InputStream is = socket.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);

      // Read greeting from the server.
      
      String response = br.readLine();
      System.out.println(response);
      if (!response.startsWith("220")) {
         throw new Exception("220 reply not received from server.");
      }

      // Get a reference to the socket's output stream.
      OutputStream os = socket.getOutputStream();

      // Send HELO command and get server response.
      String command = "HELLO Nikki\r\n";
      System.out.print(command);
      os.write(command.getBytes("US-ASCII"));
      response = br.readLine();
      System.out.println(response);
      if (!response.startsWith("250")) {
         throw new Exception("250 reply not received from server.");
      }

      // Send MAIL FROM command.
	      

      // Send RCPT TO command.
      
	
      // Send DATA command.
      

      // Send message data.
      
      // End with line with a single period.
      

      // Send QUIT command.
      
   }
}
 
Well, you're sending your commands to System.out, and only sending the first command to os.write... so you've only got the sender, not the recipient or the message itself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top