0

I am getting the following exception while trying send an email from java via gmail.

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at Mail.Send(Mail.java:75)
at Mail.main(Mail.java:85)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 5 more

I referred http://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm and Send email using java but same exception is coming both for SSL and TLS. Any fix for this?

Thanks in advance.

Community
  • 1
  • 1
Leo
  • 5,017
  • 6
  • 32
  • 55

2 Answers2

0

You will need to enable SMTP access in GMAIL, also you need to allow unsecure clients for your account:

https://www.google.com/settings/security/lesssecureapps

https://support.google.com/mail/answer/75726

Be aware that the smtp port you want to use is open in your firewall! I suggest to try a connection check from the machine where your code is running with Thunderbird if possible. Then you know if the client is able to connect.

LucasF
  • 883
  • 1
  • 6
  • 17
  • Concerning the Answer from Pracede: Port 465 is allowed with SSL/TLS by gmail, so this is not the reason why this is not working. But you are right, STARTTLS is the safer way, of course. – LucasF Dec 04 '14 at 09:20
  • I checked with Thunderbird, but it's also failing to connect to gmail. – Leo Dec 04 '14 at 09:46
  • Did you allow unsecure apps here: https://www.google.com/settings/security/lesssecureapps? Otherwise Thunderbird will not work. You will only get: Password not accepted in Thunderbird. – LucasF Dec 04 '14 at 11:02
  • Yes I did but failed to connect. – Leo Dec 04 '14 at 11:23
  • Which ports did you try? 25, 465, 587? Are you in a company network? How about firewalls? Please give more information. Otherwise I am wasting my time here. – LucasF Dec 04 '14 at 13:19
  • I tried all these ports...Yes, I'm in a company network. And I can't change the firewall settings. Is it due to firewall? – Leo Dec 05 '14 at 06:14
  • Since there no message from GMAIL that they blocked an unsecure client, they never recieved a request from Thunderbird or our application. I think it a firewall issue. Maybe you can talk to your network administrator or try our application from another network. – LucasF Dec 05 '14 at 09:55
0

After you to enable SMTP access in GMAIL, also you need to allow unsecure clients for your account ( as suggested by LucasF) Try this configuration :

host :smtp.gmail.com
port :587
username :you@gmail.com
password :yourpassword
   JavaMailProperties : 
     mail.transport.protocol : smtp
     mail.smtp.auth : true
     mail.smtp.starttls.enable :true
     mail.debug :true

Me I use this configuration with import org.springframework.mail.SimpleMailMessage class and it's works.

Pracede
  • 4,226
  • 16
  • 65
  • 110
  • @Pracede...Also used 587...but same exception is coming. – Leo Dec 04 '14 at 11:32
  • The problem is, that he can't even connect with Thunderbird. There are two possible reasons for this: Ports on the Client or Network are blocked or Gmail does not accept the connection because of an unsecure or blocked client. When Gmail is blocking a client, you will have a email message concerning this issue in your Gmail-Inbox. – LucasF Dec 04 '14 at 23:29
  • No I don't get any message from gmail – Leo Dec 05 '14 at 06:16