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

Discussion in 'Java' started by jrefactors@hotmail.com, Feb 21, 2005.

  1. Guest

    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;
    }
     
    , Feb 21, 2005
    #1
    1. Advertising

  2. On 21 Feb 2005 09:02:41 -0800, wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Feb 21, 2005
    #2
    1. Advertising

  3. Tom Dyess Guest

    <> wrote in message
    news:...
    >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?
    --
    Tom Dyess
    OraclePower.com
     
    Tom Dyess, Feb 21, 2005
    #3
  4. Steve Sobol Guest

    Tom Dyess wrote:

    > 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 / / 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)
     
    Steve Sobol, Feb 21, 2005
    #4
  5. wrote:
    > 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
     
    Steve Horsley, Feb 21, 2005
    #5
  6. Daniel Tahin Guest

    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 --> answer: 530 5.7.0 Must issue a STARTTLS
    command first

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




    Steve Horsley wrote:
    > wrote:
    >
    >> 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
     
    Daniel Tahin, Feb 21, 2005
    #6
  7. Tilman Bohn Guest

    [Followup-To set to clj.help]

    In message <421a2d98$0$11610$>,
    Daniel Tahin wrote on Mon, 21 Feb 2005 19:51:04 +0100:

    [...]
    > MAIL FROM --> answer: 530 5.7.0 Must issue a STARTTLS
    > command first
    >
    > Does this mean, we should use TLS on port 25?


    Yes.

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Feb 21, 2005
    #7
  8. Steve Sobol Guest

    Steve Horsley wrote:
    > 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 / / 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)
     
    Steve Sobol, Feb 21, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CptAmerica
    Replies:
    3
    Views:
    3,094
    CptAmerica
    Aug 19, 2003
  2. Guy Ka
    Replies:
    0
    Views:
    19,183
    Guy Ka
    Oct 20, 2003
  3. Gasolinera
    Replies:
    1
    Views:
    1,661
    Sudsy
    Nov 22, 2003
  4. networm
    Replies:
    3
    Views:
    18,461
  5. ashish makani
    Replies:
    5
    Views:
    698
    Mark Lawrence
    Sep 20, 2012
Loading...

Share This Page