I try to create my own email client. Before I start creating a GUI etc I try to get the fundamentals working. Sending an email over gmail servers is not working.
I establish a connection to the host smtp.gmail.com on port 465 over a SSL socket. After I send the EHLO command the server replies with a message. But all other messages are ignored and my reading on the socket times out.
Why does the server reply to EHLO but then doesn't respond anymore?
Here is my console output:
Successfully connected to smtp.gmail.com:465.
EHLO gmail.com
220 smtp.gmail.com ESMTP p15sm5794363ejx.109 - gsmtp
MAIL FROM:<[email protected]>
java.net.SocketTimeoutException: Read timed out
And here is the interesting part of my client code:
public void connect(String ip, int port, int timeout) {
try {
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = (SSLSocket) sslSocketFactory.createSocket(ip, port);
socket.setSoTimeout(timeout);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Successfully connected to " + ip + ":" + port + ".");
} catch (IOException e) {
System.out.println("Error: Can't connect to " + ip + ":" + port + ".");
e.printStackTrace();
}
}
public String sendMessage(String msg) {
out.print(msg + "\r\n");
String resp = "";
try {
resp = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
In the main function I just read input from the console, send it with sendMessage() and print the responds in a loop. The socket timeout is set to 20 seconds.
Edit: I don't want to use any 3rd party library or javax.mail.