25

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 ?

Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
kuba44
  • 1,838
  • 9
  • 34
  • 62
  • Connect timeout and read timeout are two different things, not the same thing. You've set one and experienced the other. – user207421 Sep 23 '13 at 23:40
  • so how to set read timeout in this situation ? – kuba44 Sep 23 '13 at 23:46
  • Do you still get a long timeout when starttls is not enabled? I think there might be a bug in Java Mail where the timeout becomes very long when starttls enabled. – Leigh McCulloch Sep 24 '13 at 05:26
  • what _starttls_ mean ? – kuba44 Sep 24 '13 at 09:11
  • see this, http://stackoverflow.com/questions/7557655/javamail-socket-read-timeout – Howard Sep 27 '13 at 05:24
  • I saw this, but there isn't any resolve. I don't use the classes in JavaMail package directly. I use API like `Session session = Session.getInstance(prop, null); Transport transport = session.getTransport("smtp"); transport.connect()` – kuba44 Sep 30 '13 at 10:32

6 Answers6

23

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.

Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
  • 1
    when i replace `prop.put("mail.smtp.connectiontimeout", 1000);` by `prop.put("mail.smtp.timeout", 1000);` to my code, nothing happened. I have to wait 5 min **again** to catch `javax.mail.MessagingException: java.net.SocketTimeoutException: Read timed out` – kuba44 Sep 23 '13 at 23:45
  • 1
    What version of JavaMail are you using? If you turn on Session debugging, what does the debug output show? – Bill Shannon Sep 24 '13 at 00:20
  • JavaMail 1.4 version, – kuba44 Sep 24 '13 at 10:42
  • and if i turn debugging it stop at `transport.connect("mion.elka.pw.edu.pl", 587, registerLog, registerPass);` and after 5 min I cath: `java.net.SocketTimeoutException: Read timed out` – kuba44 Sep 24 '13 at 10:59
  • 3
    You're using a pretty old version of JavaMail. Try using strings instead of integers when setting the timeout properties. Older versions of JavaMail only handled strings correctly. – Bill Shannon Sep 24 '13 at 19:35
  • unfortunately changing values to strings instead of integers doesn't help, still I have to wait 5 min. Do you think that change to newer version fix it ? – kuba44 Sep 24 '13 at 22:07
  • It might be the firewall issue. Can you confirm the firewall settings? – Shamim Ahmmed Sep 26 '13 at 14:46
  • I use Ubuntu, and by default, the firewall on Ubuntu (which can't be removed, because its part of the kernel) is unconfigured, and has default allow on everything. So it isn't firewall – kuba44 Sep 30 '13 at 10:27
15

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+).

J. Snow
  • 685
  • 7
  • 15
  • I have tried both smtp, smtps. nothing is working, it is taking around 7 seconds. The version of javax-mail is 1.6.2 – Satish Patro Jul 02 '20 at 14:18
11

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");    
kuro
  • 3,214
  • 3
  • 15
  • 31
  • 2
    According to [the JavaDocs](https://javaee.github.io/javamail/docs/api//com/sun/mail/smtp/package-summary.html): The SMTP protocol provider supports the following properties, which may be set in the JavaMail `Session` object. **The properties are always set as strings**; the Type column describes how the string is interpreted. – MikeOnline Oct 30 '17 at 19:09
7

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

robbyone
  • 79
  • 1
  • 1
  • could you please be more specific with the answer? – Mysterion Jan 29 '15 at 10:52
  • 1
    @robbyone Look at my comment below shamimz's answer. On Sep 24 '13 at 22:07 i wrote: "unfortunately changing values to strings instead of integers doesn't help, still I have to wait 5 min. Do you think that change to newer version fix it ?" – kuba44 Jan 30 '15 at 11:55
7

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.

indev
  • 125
  • 2
  • 8
1

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 :)

kuba44
  • 1,838
  • 9
  • 34
  • 62