SMTP Email in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilbit02
    New Member
    • Nov 2007
    • 22

    SMTP Email in Java

    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.
          
       }
    }
  • lilbit02
    New Member
    • Nov 2007
    • 22

    #2
    I did get my code to compile and run. I also established a connection with the mail server but I can't seem to get an email sent. Can someone please help.

    Code:
    import java.io.*;
    import java.net.*;
    
    public class EmailAgent
    {
       public static void main(String[] args) throws Exception
       {
          // Establish a TCP connection with the mail server.
          
          Socket TCPConnection = new Socket("mail.bellsouth.net", 25);
    
          // Create a BufferedReader to read a line at a time.
    
          InputStream is = TCPConnection.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 = TCPConnection.getOutputStream();
    
          // Send HELLO command and get server response.
          //String command = "HELLO Nikki\r\n";
         
            // Send FROM command.
          String command = "MAIL FROM:Nikki<nikki_barnard@bellsouth.net>\r\n";
          // Send RCPT TO command.
          String command2 = "RCPT TO:Nikki<misslilbit02@yahoo.com>\r\n";
          // Send DATA command.
          String command3 = "DATA\r\n";
          // Subject
          String command4 = "SUBJECT: DataComm Programming Assignment\r\n\r\n";
          // Send message data.
          String command5 = "HELLO Nikki. This is your java programming assignment for DataComm\r\n";
          // End with line with a single period.      
          String command6 = ".\r\n";
          // Send QUIT command.
          String command7 = "QUIT\r\n";
          
          System.out.print(command);
          System.out.print(command2);
          System.out.print(command3);
          System.out.print(command4);
          System.out.print(command5);
          System.out.print(command6);
    //      System.out.print(command7);      
          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.");
          }
          
       }
    }

    Comment

    • lilbit02
      New Member
      • Nov 2007
      • 22

      #3
      Nevermind I Got It To Work.

      Comment

      • vfly7
        New Member
        • Feb 2008
        • 1

        #4
        This is one of the example for this SMTP Email in Java Solution.
        Hope this will help you.
        Thanks.

        Code(EmailAgent .java):-
        import java.io.*;
        import java.net.*;
        import java.util.*;
        import java.text.*;
        import javax.swing.*;

        public class EmailAgent
        {
        public static void main (String [] args) throws Exception
        {
        //Fill in the require information
        String mailServer = ""; //Fill in the mail server address
        String to = ""; //Fill in the receiver email address
        String from = ""; //Fill in the sender email address
        String msg = ""; //Fill in the message that want to be send

        //Establish a TCP connection with the mail server
        Socket socket = new Socket(mailServ er,25);

        //Create a BufferedReader to read a line at a time.
        InputStream is = socket.getInput Stream();
        InputStreamRead er isr = new InputStreamRead er(is);
        BufferedReader br = new BufferedReader( isr);

        //Read greeting from the server
        String response = br.readLine();
        System.out.prin tln(response);

        //DataOutputStrea m
        DataOutputStrea m toServer = new DataOutputStrea m(socket.getOut putStream());

        if (!response.star tsWith("220"))
        {
        throw new Exception("220 reply not received from server.");
        }

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

        //Send HELO command and get server response.
        String command = "HELO alice\r\n";
        System.out.prin t(command);
        os.write(comman d.getBytes("US-ASCII"));
        response = br.readLine();
        System.out.prin tln(response);
        if (!response.star tsWith("250"))
        {
        throw new Exception("250 reply not received from server.");
        }

        //Send MAIL FROM command
        String cmdFrom = "MAIL FROM:<" + from + ">\r\n";
        toServer.writeB ytes(cmdFrom);
        response = br.readLine();
        System.out.prin tln(response);

        //Send RCPT TO command
        String cmdTo = "RCPT TO:<" + to + ">\r\n";
        toServer.writeB ytes(cmdTo);
        response = br.readLine();
        System.out.prin tln(response);

        //Send DATA command
        String cmdData = "DATA\r\n";
        toServer.writeB ytes(cmdData);
        response = br.readLine();
        System.out.prin tln(response);

        //Send message data
        String cmdMsg = msg + "\r\n";

        //Date
        SimpleDateForma t format = new SimpleDateForma t("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
        String dateString = format.format(n ew Date());
        String cmdDate = "Date: " + dateString + "\r\n";

        //Send write to fill in email form
        String cmdAll = "From: " + from + "\r\n" + "To: " + to + "\r\n" + cmdDate + "\r\n" + cmdMsg + ".";
        toServer.writeB ytes(cmdAll);

        //End with line with a single period
        String cmdCrlf = "\r\n";
        toServer.writeB ytes(cmdCrlf);
        response = br.readLine();
        System.out.prin tln(response);

        //Send Quit command
        String cmdQuit = "QUIT\r\n";
        toServer.writeB ytes(cmdQuit);
        response = br.readLine();
        System.out.prin tln(response);

        socket.close();

        System.out.prin tln(cmdFrom);
        System.out.prin tln(cmdTo);
        System.out.prin tln(cmdData);
        System.out.prin tln(cmdMsg);
        System.out.prin tln(cmdQuit);
        System.out.prin tln(cmdCrlf);
        }
        }

        Comment

        • midofidodido
          New Member
          • Nov 2008
          • 1

          #5
          Originally posted by lilbit02
          Nevermind I Got It To Work.
          Well, me too had the same problem, i used this code, and still no mail is being sent.
          Can you help me regarding this ?

          Thanks,
          Mohamed

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Why don't you people use Sun's Java Mail API?

            kind regards,

            Jos

            Comment

            • N002213F
              New Member
              • Sep 2007
              • 38

              #7
              Originally posted by JosAH
              Why don't you people use Sun's Java Mail API?

              kind regards,

              Jos
              Finally someone sees the light

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by N002213F
                Finally someone sees the light
                I've decided to put the download link in the "Read This First" article; people seem
                to be unable to google for it themselves. Our solar system must be floating through
                a huge cloud of silly particles and we haven't hit the center yet I'm afraid.

                kind regards,

                Jos

                Comment

                Working...