send SMTP email using socket problem: java.net.ConnectException

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;
}
 
G

Gordon Beaton

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

Your attempt to connect to smtp.gmail.com:465 has failed.

The fact that you got a timeout (and not connection refused) seems to
indicate that the connection attempt was quietly dropped by a filter
somewhere between your host and their server, or maybe it's just slow.
Did you try port 25?

I am not a Gmail user, but have read that port numbers mentioned on
their help site might be wrong. Try 587 as well. Search Google for
"gmail port 465" and you'll discover others experiences with
connecting to Gmail. Many hits point here:

http://www.gmailforums.com/lofiversion/index.php/f5.html

/gordon
 
T

Tom Dyess

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;
}

Are you sure you want smtp.gmail.com at port 465? SMTP is usually port 25.
At which line do you get the Connection timed out: connect error? On the
Socket constructor? Try it with a SMTP server you know works. Do you know
how to set it up on your development box?
 
S

Steve Sobol

Tom said:
Are you sure you want smtp.gmail.com at port 465? SMTP is usually port 25.

SMTP over SSL is port 465... maybe that's the problem? Is the code example
supposed to use SSL?

--
JustThe.net - Apple Valley, CA - http://JustThe.net/ - 888.480.4NET (4638)
Steven J. Sobol, Geek In Charge / (e-mail address removed) / PGP: 0xE3AE35ED

"In case anyone was wondering, that big glowing globe above the Victor
Valley is the sun." -Victorville _Daily Press_ on the unusually large
amount of rain the Southland has gotten this winter (January 12th, 2005)
 
S

Steve Horsley

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

I don't think gmail allow you to send mail messages through their server using
SMTP at all. They allow POP3 to retrieve messages though.

You can prove by hand that they don't accept SMTP with this command:
telnet smtp.google.com 465

Steve
 
D

Daniel Tahin

Yes, telnet is a good idea. I tried telnet smtp.gmail.com 25 and it
seems to be ok.
HELO --> answer: 250 smtp.gmail.com at your service
MAIL FROM (e-mail address removed) --> answer: 530 5.7.0 Must issue a STARTTLS
command first

Does this mean, we should use TLS on port 25?




Steve said:
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


I don't think gmail allow you to send mail messages through their server
using
SMTP at all. They allow POP3 to retrieve messages though.

You can prove by hand that they don't accept SMTP with this command:
telnet smtp.google.com 465

Steve
 
T

Tilman Bohn

[Followup-To set to clj.help]

In message <[email protected]>,
Daniel Tahin wrote on Mon, 21 Feb 2005 19:51:04 +0100:

[...]
MAIL FROM (e-mail address removed) --> answer: 530 5.7.0 Must issue a STARTTLS
command first

Does this mean, we should use TLS on port 25?

Yes.
 
S

Steve Sobol

Steve said:
I don't think gmail allow you to send mail messages through their server
using
SMTP at all. They allow POP3 to retrieve messages though.

You can prove by hand that they don't accept SMTP with this command:
telnet smtp.google.com 465

That just means they don't do SMTP over SSL.


--
JustThe.net - Apple Valley, CA - http://JustThe.net/ - 888.480.4NET (4638)
Steven J. Sobol, Geek In Charge / (e-mail address removed) / PGP: 0xE3AE35ED

"In case anyone was wondering, that big glowing globe above the Victor
Valley is the sun." -Victorville _Daily Press_ on the unusually large
amount of rain the Southland has gotten this winter (January 12th, 2005)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top