I am sending mail from my Java app to Gmail Account. I had used the Java Mail API and it worked fine. But is it possible to send an e-mail without using the mail API in java?
I mean just by using sockets:
public class Main {
public static void main(String[] args) throws Exception {
String host = "smtp.gmail.com";
int port = 465;
String from = "[email protected]";
String toAddr = "[email protected]";
Socket servSocket = new Socket(host, port);
DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
DataInputStream is = new DataInputStream(servSocket.getInputStream());
if (servSocket != null && os != null && is != null) {
os.writeBytes("HELO
");
os.writeBytes("MAIL From:" + from + "
");
os.writeBytes("RCPT To:" + toAddr + "
");
os.writeBytes("DATA
");
os.writeBytes("X-Mailer: Java
");
os.writeBytes("DATE: " + DateFormat.getDateInstance(DateFormat.FULL,
Locale.US).format(new Date()) + "
");
os.writeBytes("From:" + from + "
");
os.writeBytes("To:" + toAddr + "
");
}
os.writeBytes("Subject:
");
os.writeBytes("body
");
os.writeBytes("
.
");
os.writeBytes("QUIT
");
String responseline;
while ((responseline = is.readUTF()) != null) {
if (responseline.indexOf("Ok") != -1)
break;
}
}
}
But it is not working, it doesn't send out the mail. Can anyone tell me what could be the problem?
See Question&Answers more detail:os