Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Java socket connect to gmail

public class TotalTemp
{
     private static DataOutputStream dos;

     public static void main(String[] args) throws Exception
     {
          int delay = 1000;
          String user = "xxxxx@gmail.com";
          String pass = "xxxxxxxx11";
          String username = Base64.encodeBase64String(user.getBytes(StandardCharsets.UTF_8));
          String password = Base64.encodeBase64String(pass.getBytes(StandardCharsets.UTF_8));    

          SSLSocket sock = (SSLSocket)((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket("smtp.gmail.com", 465);
//          Socket sock = new Socket("smtp.gmail.com", 587);
          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
");
          Thread.sleep(delay);
          send("AUTH LOGIN
");
          Thread.sleep(delay);
          send(username + "
");
          Thread.sleep(delay);
          send(password + "
");
          Thread.sleep(delay);
          send("MAIL FROM:<XXXXXXXX@gmail.com>
");
          //send("
");
          Thread.sleep(delay);
          send("RCPT TO:<YYYYYYYY@gmail.com>
");
          Thread.sleep(delay);
          send("DATA
");
          Thread.sleep(delay);
          send("Subject: Email test
");
          Thread.sleep(delay);
          send("Test 1 2 3
");
          Thread.sleep(delay);
          send(".
");
          Thread.sleep(delay);
          send("QUIT
");
     }

     private static void send(String s) throws Exception
     {
          dos.writeBytes(s);
          System.out.println("CLIENT: "+s);
     }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Java #socket #connect #gmail
ADD COMMENT
Topic
Name
7+3 =