Results 1 to 12 of 12
  1. #1
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Unhappy javamail, Connection timed out

    hi,

    I am trying to send email, I think something blocks it.

    I keep getting this error :
    Java Code:
    Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 25;
      nested exception is:
            java.net.ConnectException: Connection timed out: connect
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    ...
    it seems like it is a firewall problem, but i turned off my antivirus and firewall. when I try to connect from cmd (telnet smtp.live.com 25) it cant connect either. I tried to unblock port 25 but I couldnt make it either, I put exception to windows firewall(it is already turned off) it still cant connect thru that port.

    but when i type telnet smtp.live.com 587 to command promt, it connects..

    how do I overcome this problem? or how do I connect over port 587 in java?

    here is my code:
    Java Code:
    public class SendMailUsingAuthentication
    {
    
      private static final String SMTP_HOST_NAME = "smtp.live.com";
      private static final String SMTP_AUTH_USER = "asd@hotmail.com";
      private static final String SMTP_AUTH_PWD  = "asdasd";
    
      private static final String emailMsgTxt      = "msg text";
      private static final String emailSubjectTxt  = "subject txt";
      private static final String emailFromAddress = "asd@yahoo.com";
    
      // Add List of Email address to who email needs to be sent to
      private static final String[] emailList = {"asd@hotmail.com", "qwe@hotmail.com"};
    
      public static void main(String args[]) throws Exception
      {
        SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
        smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
      }
    
      public void postMail( String recipients[ ], String subject,
                                String message , String from) throws MessagingException
      {
        boolean debug = false;
    
         //Set the host smtp address
         Properties props = new Properties();
         props.put("mail.smtp.host", SMTP_HOST_NAME);
         props.put("mail.smtp.auth", "true");
         
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
    
        session.setDebug(debug);
    
        // create a message
        Message msg = new MimeMessage(session);
    
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
    
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
    
    
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
     }
    
    
    /**
    * SimpleAuthenticator is used to do simple authentication
    * when the SMTP server requires it.
    */
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
    
        public PasswordAuthentication getPasswordAuthentication()
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
    
    }
    thanks..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by OmerHalit View Post
    hi,

    I am trying to send email, I think something blocks it.

    I keep getting this error :
    [CODE]Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 25;
    nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1706)
    Changing the port to use is easy:

    Java Code:
    String port= "587";
    ...
    Properties props = System.getProperties();
    props.put("mail.smtp.port", port);
    If you really want to know what's going on between your client and the smtp server, do this:

    Java Code:
    session.setDebug(true);
    kind regards,

    Jos

  3. #3
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default

    thank you, it worked for that error.. :) this is the another error I got..

    Java Code:
    Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
    
            at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
            at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
            at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
            at javax.mail.Transport.send0(Transport.java:191)
            at javax.mail.Transport.send(Transport.java:120)
            at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:100)
            at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:61)
    Java Result: 1
    when i connected to smtp thru command prompt, whatever I type, it used to give me the same error..

    i think connecting to the smtp is ok.. but i can't send the mail, is it because i don't use proper command? :confused:

    i connected to smtp thru command prompt, when i typed "STARTTLS" and press enter, it says "Send hello first". what does it mean?

    thank you again..
    Last edited by OmerHalit; 04-05-2010 at 06:01 AM.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I'm sure it didn't say send Hello first, but rather send HELO first right?

    There are two things I remember from when I worked with this...

    Try adding another property:
    Java Code:
    props.put("mail.smtp.ehlo","true");
    also I remember creating an instance of Transport to send the message, something like this
    Java Code:
    Transport transport = session.getTransport("smtp");
    transport.connect();//You could specify the Server Address, User Name and Password when connecting but I don't think you need it
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    Hope this helps.

  5. #5
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default



    it really says that..

    ok, i will try what you said.. give me a while to try..

    Thanks..

  6. #6
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default

    I tried, it didn't work.. :(

    what else could be?

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I never tried connecting to a hotmail account. Let me create one and attempt it.

  8. #8
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default

    ok, here is the last looking of my code
    ...
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    //everything is same until here

    //here was changed according to StormyWaters' suggestion

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport transport = session.getTransport("smtp");
    transport.connect();
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    //Transport.send(msg);

    I get no error until this line : transport.sendMessage(msg, msg.getAllRecipients());

    means that, i can connect, but i cannot send the mail.. :( any help??

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Forgot a property, try adding this
    Java Code:
    props.put("mail.smtp.starttls.enable","true");

  10. #10
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default

    You are great...!!!

    Thanks, it worked.. :)
    Last edited by OmerHalit; 04-06-2010 at 07:19 PM.

  11. #11
    xumingming is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Unhappy how to receive hotmail

    how to receive hotmail? when I user store to receive the mail,always to remind me Connect failed;

  12. #12
    OmerHalit is offline Member
    Join Date
    Mar 2010
    Posts
    41
    Rep Power
    0

    Default

    How do you connect? did you compare your code with the one I sent?

Similar Threads

  1. Connection Timed Out
    By sukatoa in forum Networking
    Replies: 2
    Last Post: 10-27-2009, 03:20 PM
  2. connection timed out in my java code
    By santhosh_el in forum AWT / Swing
    Replies: 4
    Last Post: 10-22-2009, 12:24 PM
  3. solution for connection timed out
    By santhosh_el in forum Networking
    Replies: 0
    Last Post: 10-03-2009, 07:55 AM
  4. i am getting connection timed out
    By santhosh_el in forum Networking
    Replies: 1
    Last Post: 08-25-2009, 06:57 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •