0

I have a java application which sends email.It have been working well never had this problem. Suddenly today I see a lot of these error, but there are some which went through successfully.Below is the full trace.

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection timed out
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at commServer9000$MailProcessor.run(commServer9000.java:6550)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 8 more
user837306
  • 857
  • 3
  • 18
  • 32
  • 2
    Yes, the internet is not perfect. You should probably be expecting things like that to happen, and dealing with them accordingly. Catch Exception, retry, bail after X retries. – Brian Roach Mar 15 '12 at 13:16
  • It might be because of network issue. Please have a look at following, http://stackoverflow.com/questions/1990454/using-javamail-to-connect-to-gmail-smtp-server-ignores-specified-port-and-tries This might solve your issue – Rahul Borkar Mar 15 '12 at 13:23
  • It is definitely network issue, as Connection is getting timed out. Or there is problem at server side. – Rahul Borkar Mar 15 '12 at 13:24
  • yes I did this on the server and my home pc telnet smtp.gmail.com 25 both getting connection timed out. so I guess nothing wrong with the codes is just the connectivity today is the problem am I right? – user837306 Mar 15 '12 at 14:50
  • Gmail doesn't listen on port 25, so that's not a valid test. Use port 465. Still, it seems clear that you have network problems. – Bill Shannon Mar 15 '12 at 18:08
  • @bill my application is crucial to send email.so those failed email should I like store and retry later using another service to try to sending them is it. – user837306 Mar 16 '12 at 01:57
  • If your network is so unreliable that you can't depend on getting a connection to Gmail, you'll need a way to queue the messages and send them later. The simplest way to do that might be to install your own mail server, send the messages to it, and let it handle the unreliable network connection to Gmail. – Bill Shannon Mar 16 '12 at 06:51
  • @bill when you save queue is it means to put them into a db else how to queue it? Yes we are planning to send directly via our own mail server will be better right? – user837306 Mar 16 '12 at 12:27
  • If you're using your own mail server, it will do the queuing for you. If you have to do the queuing yourself, you could store the messages in a database, but it's usually a lot easier to store them in files. – Bill Shannon Mar 16 '12 at 17:18
  • @bill store them in which type of file do your mean text files? If I am using my own mail server where will it queue is it a feature of the mail server or we need to set ? – user837306 Mar 17 '12 at 18:44
  • Obviously you have **much** to learn. The JavaMail FAQ will provide some more background information. Yes, text files. Yes, a mail server will manage its own queue, usually in files of its own, sometimes in a SQL database that you provide. – Bill Shannon Mar 17 '12 at 23:04

1 Answers1

0

In this case, enabling SSL solved my problem.

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(465);

//setting username and password
mailSender.setUsername("UserName");
mailSender.setPassword("Password");

//setting Spring JavaMailSenderImpl Properties
Properties mailProp = mailSender.getJavaMailProperties();

mailProp.put("mail.transport.protocol", "smtp");
mailProp.put("mail.smtp.auth", "true");
mailProp.put("mail.smtp.starttls.enable", "true");
mailProp.put("mail.smtp.starttls.required", "true");
mailProp.put("mail.debug", "true");
mailProp.put("mail.smtp.ssl.enable", "true");
mailProp.put("mail.smtp.user", String.valueOf(resourceList.get(0)));
barbsan
  • 3,418
  • 11
  • 21
  • 28
Pradeep Kumar
  • 109
  • 1
  • 10