4 Replies Latest reply: Nov 5, 2012 1:36 AM by Kayaman RSS

    Java. Sending email without using java mail libraries? (socket to gmail )

    972346
      Hello)))
      I am trying to send a mail, creating a socket to gmail smtp server.
      However, I get exception

      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.Socket;
      import javax.net.ssl.SSLSocket;
      import javax.net.ssl.SSLSocketFactory;

      import org.apache.commons.codec.binary.Base64;
      public class TotalTemp
      {
           private static DataOutputStream dos;
           
           public static void main(String[] args) throws Exception
           {
                int delay = 1000;
                String username = "xyz@gmail.com";
                String password ="mypassword";
                

      SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
      SSLSocket sock = (SSLSocket) sslsocketfactory.createSocket( "smtp.gmail.com", 465);

                final BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                (new Thread(new Runnable()
                {
                     public void run()
                     {
                          try
                          {
                               String line;
                               while((line = br.readLine()) != null)
                                    System.out.println("SERVER: "+line);
                          }
                          catch (IOException e)
                          {
                               e.printStackTrace();
                          }
                     }
                })).start();
                dos = new DataOutputStream(sock.getOutputStream());
                
                send("EHLO smtp.gmail.com\r\n");
                Thread.sleep(delay);
                send("AUTH LOGIN\r\n");
                Thread.sleep(delay);
                send(username.toString());
                Thread.sleep(delay);
                send(password.toString());
                Thread.sleep(delay);
                send("MAIL FROM: <xyz@gmail.com>\r\n");
                          //send("\r\n");
                Thread.sleep(delay);
                send("RCPT TO: xyz@gmail.com\r\n");
                Thread.sleep(delay);
                send("DATA\r\n");
                Thread.sleep(delay);
                send("Subject: Email test\r\n");
                Thread.sleep(delay);
                send("Test 1 2 3");
                Thread.sleep(delay);
                send("\r\n.\r\n");
                Thread.sleep(delay);
                send("QUIT\r\n");
           }
           
           private static void send(String s) throws Exception
           {
                dos.writeBytes(s);
                System.out.println("CLIENT: "+s);
           }
      }


      SERVER: 220 mx.google.com ESMTP s1sm34459030eem.9
      CLIENT: EHLO smtp.gmail.com

      SERVER: 250-mx.google.com at your service, [46.162.218.31]
      SERVER: 250-SIZE 35882577
      SERVER: 250-8BITMIME
      SERVER: 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
      SERVER: 250 ENHANCEDSTATUSCODES
      CLIENT: AUTH LOGIN

      SERVER: 334 VXNlcm5hbWU6
      CLIENT: xyz@gmail.com
      CLIENT: mypassword
      CLIENT: MAIL FROM: <xyz@gmail.com>

      SERVER: 501 5.5.2 Cannot Decode response s1sm34459030eem.9
      CLIENT: RCPT TO: xyz@gmail.com

      SERVER: 530-5.5.1 Authentication Required. Learn more at
      SERVER: 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 s1sm34459030eem.9
      CLIENT: DATA

      SERVER: 530-5.5.1 Authentication Required. Learn more at
      SERVER: 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 s1sm34459030eem.9
      CLIENT: Subject: Email test

      SERVER: 502 5.5.1 Unrecognized command. s1sm34459030eem.9
      CLIENT: Test 1 2 3
      CLIENT:
      .

      SERVER: 502 5.5.1 Unrecognized command. s1sm34459030eem.9
      SERVER: 502 5.5.1 Unrecognized command. s1sm34459030eem.9
      CLIENT: QUIT

      SERVER: 221 2.0.0 closing connection s1sm34459030eem.9



      Can you please help????))))