J
jrefactors
I want to write the socket program to send SMTP email, without using
any JavaMail API.
Here's the program, but it turns out it has the following exceptions: I
don't know
which part it went wrong. please advise. thanks!!
/**
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at SMTPTest.sendMail(SMTPTest.java:20)
at SMTPTest.main(SMTPTest.java:12)
*/
======================================================================
import java.net.*;
import java.io.*;
import java.util.*;
/**
* send SMTP email using socket
*
*/
public class SMTPTest
{ public static void main(String[] args)
{ SMTPTest smtp = new SMTPTest();
smtp.sendMail();
}
public void sendMail()
{
try
{
Socket s = new Socket("smtp.gmail.com", 465); //smtp.gmail.com, 465
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();
System.out.println("hostName = " + hostName);
send(null);
send("HELO " + hostName);
send("MAIL FROM: " + "my gmail email");
send("RCPT TO: " + "my gmail email");
send("DATA");
send("Happy SMTP Programming!!");
send("Happy SMTP Programming!!");
send(".");
send("QUIT");
s.close();
out.close();
in.close();
}
catch(IOException e)
{ e.printStackTrace();
}
}
public void send(String s) throws IOException
{ if (s != null)
{ out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null) //output the response
System.out.println(line);
}
private PrintWriter out;
private BufferedReader in;
}
any JavaMail API.
Here's the program, but it turns out it has the following exceptions: I
don't know
which part it went wrong. please advise. thanks!!
/**
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at SMTPTest.sendMail(SMTPTest.java:20)
at SMTPTest.main(SMTPTest.java:12)
*/
======================================================================
import java.net.*;
import java.io.*;
import java.util.*;
/**
* send SMTP email using socket
*
*/
public class SMTPTest
{ public static void main(String[] args)
{ SMTPTest smtp = new SMTPTest();
smtp.sendMail();
}
public void sendMail()
{
try
{
Socket s = new Socket("smtp.gmail.com", 465); //smtp.gmail.com, 465
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();
System.out.println("hostName = " + hostName);
send(null);
send("HELO " + hostName);
send("MAIL FROM: " + "my gmail email");
send("RCPT TO: " + "my gmail email");
send("DATA");
send("Happy SMTP Programming!!");
send("Happy SMTP Programming!!");
send(".");
send("QUIT");
s.close();
out.close();
in.close();
}
catch(IOException e)
{ e.printStackTrace();
}
}
public void send(String s) throws IOException
{ if (s != null)
{ out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null) //output the response
System.out.println(line);
}
private PrintWriter out;
private BufferedReader in;
}