Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: what's wrong with this code

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong with this code

    import javax.mail.*;
     
    import javax.mail.internet.*;
    import java.util.Properties;
    public class Access
    {
    public static void main (String args[])
    {
    int argc = args.length;
    if (argc != 4)
    {
    System.out.println ("Syntax :");
    System.out.println (
    "java Access protocol host username password");
    return;
    }
    String protocol = args[0];
    String host = args[1];
    String username = args[2];
    String password = args[3];
    try
    {
    Properties props=System.getProperties();
     
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imap.connectiontimeout", "50000");
    props.setProperty("mail.imap.timeout", "50000");
    Session mySession = Session.getDefaultInstance(props, null);
     
    Store myStore = mySession.getStore(protocol);
    myStore.connect (host, username, password);
    /***** exception is thrown here
    and exception is 
     
     
    javax.mail.MessagingException: Connection timed out: connect;
      nested exception is:
            java.net.ConnectException: Connection timed out: connect
            at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
            at javax.mail.Service.connect(Service.java:295)
            at javax.mail.Service.connect(Service.java:176)
            at Access.main(Access.java:279)
    Caused by: java.net.ConnectException: Connection timed out: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(Unknown Source)
            at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
            at java.net.PlainSocketImpl.connect(Unknown Source)
            at java.net.SocksSocketImpl.connect(Unknown Source)
            at java.net.Socket.connect(Unknown Source)
            at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:286)
            at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
            at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
            at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110)
     
            at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632)
    ******/
     
    }
    catch (MessagingException exe)
    {
    	exe.printStackTrace();
    }
    catch (Exception ex)
    {
    	ex.printStackTrace();
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what's wrong with this code

    What would the response be if the server didn't exist or it didn't want to communicate with you?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: what's wrong with this code

    Is there a question here?

    If it's regarding javax.mail.MessagingException: Connection timed out: connect; then it's self-explanatory.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong with this code

    /****************************
    this code is for connecting to gmail through java program
    this is throwing ConnectedTimeout exception at the commented place 
    please give some suggestions to solve this
    ********************************/
     
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Properties;
    public class Access
    {
    public static void main (String args[])
    {
    int argc = args.length;
     
    if (argc != 4)
    {
    System.out.println ("Syntax :");
    System.out.println (
    "java Access protocol host username password");
    return;
    }
    String protocol = args[0];
    String host = args[1];
    String username = args[2];
    String password = args[3];
    try
    {
     
    Properties props=System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imap.timeout", "50000");
    Session mySession = Session.getDefaultInstance(props, null);
     
     
    Store myStore = mySession.getStore(protocol);
    myStore.connect (host, username, password);
    /****************************************************
     
    at this line
    throwing connection timedout exception
    host,user password and iinternet coneection are provided correctly
    *******************************************************/
     
    Folder myFolder = myStore.getFolder("INBOX");
    if (myFolder == null)
    {
    System.err.println ("No default folder available");
    return;
    }
    System.out.println ("Accessing " +
    myFolder.getFullName() + " folder");
     
    myFolder.open(Folder.READ_ONLY);
    int messagecount = myFolder.getMessageCount();
    System.out.println (myFolder.getFullName() + " has "
    + messagecount +
    " messages.");
    Message[] message = myFolder.getMessages ();
    for (int i = 0; i < message.length; i++)
    {
    Address[] fromAddr = message[i].getFrom();
    System.out.println (fromAddr[0] + ":" +
    message[i].getSubject());
    }
    myFolder.close(false);
    }
    catch (MessagingException me)
    {
    System.err.println ("Messaging failure : " + me);
    }
    catch (Exception ex)
    {
    System.err.println ("Failure : " + ex);
    }
    }
    }

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: what's wrong with this code

    Please don't duplicate threads. I have merged it with this original thread.

    So you have posted code which looks pretty much the same to me? You also have given zero information about your problem.

    If you are getting a connection timeout, it would suggest that the server/username/password details are wrong.
    Are you sending these details as command line arguments when you compile the code?

    If you use the Eclipse IDE, see - http://www.javaprogrammingforums.com...s-eclipse.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: what's wrong with this code

    It looks like you are attempting to connect with null as the Authenticator. It should be throwing an AuthenticationFailedException which would be picked up by the catch exception block at the end. For the record; this is not a good idea because it can introduce bugs that are nearly impossible to track down. Try to catch the specific exception that the code may throw (your IDE will help you with this). I do not know why it would be timing out instead of throwing an exception but at a guess I would say it has something to do with google's security system. It would make sense for a massive system like gmail to timeout a bad authentication because it would make it harder to brute force someone's password (this is a un-educated guess mind you).

    You can read up on the Authenticator here.

    As a side note, your comment states: "host,user password and iinternet coneection are provided correctly". Assumptions are the cause of all f***-ups and I do not see any code that checks these parameters and fails gracefully for bad input. Given that you failed to spell 'internet' and 'connection' correctly it stands to reason you are not passing the correct parameters as command line arguments.

Similar Threads

  1. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM
  2. What's Wrong With My Code?
    By arunjib in forum What's Wrong With My Code?
    Replies: 18
    Last Post: May 7th, 2011, 08:47 PM
  3. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM
  4. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM