Frage

In my application I connect to server to authenticate users. This is code:

try {
        Properties prop = new Properties();
        prop.put("mail.smtp.starttls.enable","true");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.connectiontimeout", 1000);


        Session session = Session.getInstance(prop, null);
        Transport transport = session.getTransport("smtp");
        transport.connect("mion.elka.pw.edu.pl", 587, registerLog, registerPass);
        transport.close();
        return true;
    } catch (NoSuchProviderException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch(AuthenticationFailedException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (MessagingException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

I set connection timeout to 1000 ms = 1s but it's ignore. When i debug and set wrong username and password i catch

javax.mail.MessagingException: java.net.SocketTimeoutException: Read timed out

not after 1000 ms, but after 5000*60 ms = 5 min

What is wrong ? How can i reduce timeoute ?

War es hilfreich?

Lösung 6

I resolve my problem by changing to the newest version of JavaMail (to JavaMail 1.5). I write about it there: http://openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html

thank's everybody for help, specially to Bill Shannon :)

Andere Tipps

Can you setup the Socket I/O timeout as well. When it is connected but failed to read data from the server then it will continue to wait.

prop.put("mail.smtp.timeout", 1000);

Read timeout indicates you are connected but not able to read data from the server.

Since you're using SSL, you can try to configure smtps namespace, not smtp:

prop.put("mail.smtps.timeout", 1000);
prop.put("mail.smtps.connectiontimeout", 1000);

BTW: Timeout values in the properties can be passed as int, as well as String. JavaMail will handle them both properly (at least v1.5+).

I had the same problem. It worked with the String instead of integer.

prop.put("mail.smtp.timeout", "1000");    
prop.put("mail.smtp.connectiontimeout", "1000");    

No, it is just because value must be a string "1000" and not an integer 1000

mail.smtp.connectiontimeout
type: int
Desc: Socket connection timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.

mail.smtp.timeout type: int
Desc: Socket read timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.

mail.smtp.writetimeout type: int
Desc: Socket write timeout value in milliseconds. This timeout is implemented by using a java.util.concurrent.ScheduledExecutorService per connection that schedules a thread to close the socket if the timeout expires. Thus, the overhead of using this timeout is one thread per connection. Default is infinite timeout.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top