Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JavaMail connection timeout is too long

Tags:

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 ?

like image 834
kuba44 Avatar asked Sep 23 '13 23:09

kuba44


People also ask

How do I fix timeout error in Java?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

How can I get SMTP server response using JavaMail?

SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.

What is JavaMail used for?

The JavaMail is an API that is used to compose, write and read electronic messages (emails). The JavaMail API provides protocol-independent and plateform-independent framework for sending and receiving mails.


1 Answers

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.

like image 103
Shamim Ahmmed Avatar answered Oct 09 '22 17:10

Shamim Ahmmed