Search code examples
javaemailsocketsgmail

Sending Email using Java Sockets


I am trying to send Email using Java Sockets but cannot seem to send the Email (I am not using mail API of Java, I just want to use sockets to send Email).Please tell me where I am doing wrong and modify the code...I will be glad of any help...Thanks

Below is my code

import java.io.*;
import java.net.*;
class MailingClient {

     public void smtp(String command) throws UnknownHostException, IOException { 

         Socket socket=new Socket("smtp.gmail.com",465);

         BufferedReader br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
         br.readLine();

         OutputStream os = socket.getOutputStream();
         os.write(command.getBytes());
         smtp("HELLO " + "[email protected]");
         smtp("MAIL FROM: "+ "[email protected]");
         smtp("DATA");
         smtp("yourContent");

  }
  public static void main(String argv[]) throws Exception
    {


        MailingClient c=new MailingClient();
        c.smtp("HELO");


    }
}

Solution

  • In general this should work, you 'just' have to implement the whole SMTP protocol yourself. An introduction is available on Wikipedia. I'd really use a library for this.