GaboSampaio
GaboSampaio

Reputation: 181

Couldn't connect to host port: smtp.gmail.com 22. Even though i have port 465 set on application.properties

My application.properties:

#E-mail properties Gmail
spring.mail.host=smtp.gmail.com
[email protected]
spring.mail.password=DERP
spring.mail.port=465
spring.mail.properties.mail.smtp.auth=true

My EmailSender method(added Properties to this part to see if it would work, it didn't):

public void sendEmail(String title, String message, String email) {
        final Properties props = new Properties();
        props.put("spring.mail.port","465");
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(email);

        msg.setSubject(title);
        msg.setText(message);
        try {

        } catch (Exception e) {
            log.warn("Erro ao enviar o email para:" + email);
        }
        javaMailSender.send(msg);

    }

I thought my application wasn't accessing the application.properties for some reason, but since I'm getting values from database I'm pretty sure it's accessible. All search I've done ends up in people using the wrong port or stuff like that. The sender is in a different project i have the MailSenderCore and the one that does other logics so i thought it was because of that and tried adding an application.properties to both projects just to test it out but still the same error.

this is my log:

  nested exception is:
    java.net.ConnectException: Connection timed out (Connection timed out). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection timed out (Connection timed out)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448) ~[spring-context-support-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323) ~[spring-context-support-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312) ~[spring-context-support-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.sippulse.email.core.SipPulseEmailServiceImpl.sendEmail(SipPulseEmailServiceImpl.java:89) ~[SipPulseEmailCore-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
    at com.sippulse.email.lowcredit.SipPulseEmailLowCredit.SipPulseEmailLowCreditApplication.applicationRunner(SipPulseEmailLowCreditApplication.java:50) [classes/:na]
    at com.sippulse.email.lowcredit.SipPulseEmailLowCredit.SipPulseEmailLowCreditApplication$$EnhancerBySpringCGLIB$$6db9b225.CGLIB$applicationRunner$0(<generated>) ~[classes/:na]
    at com.sippulse.email.lowcredit.SipPulseEmailLowCredit.SipPulseEmailLowCreditApplication$$EnhancerBySpringCGLIB$$6db9b225$$FastClassBySpringCGLIB$$f2946bba.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.sippulse.email.lowcredit.SipPulseEmailLowCredit.SipPulseEmailLowCreditApplication$$EnhancerBySpringCGLIB$$6db9b225.applicationRunner(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 20 common frames omitted
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 25; timeout -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209) ~[jakarta.mail-1.6.4.jar:1.6.4]
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722) ~[jakarta.mail-1.6.4.jar:1.6.4]
    at javax.mail.Service.connect(Service.java:342) ~[jakarta.mail-1.6.4.jar:1.6.4]
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518) ~[spring-context-support-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437) ~[spring-context-support-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 34 common frames omitted
Caused by: java.net.ConnectException: Connection timed out (Connection timed out)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_242]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_242]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_242]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_242]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_242]
    at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_242]
    at java.net.Socket.connect(Socket.java:556) ~[na:1.8.0_242]
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:335) ~[jakarta.mail-1.6.4.jar:1.6.4]
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:214) ~[jakarta.mail-1.6.4.jar:1.6.4]
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160) ~[jakarta.mail-1.6.4.jar:1.6.4]
    ... 38 common frames omitted

Upvotes: 0

Views: 3147

Answers (3)

Bill Shannon
Bill Shannon

Reputation: 29971

Try the JavaMail FAQ connection debugging tips. Most likely you have a firewall or antivirus preventing you from connecting.

Upvotes: 1

Alien
Alien

Reputation: 15908

There are few points to work on.

  1. Remove email's password from the question so that someone cannot mis use of it.

  2. Enable less secure apps on Gmail here

  3. There is one more port 587 which you can use for SMTP.

  4. Enable TLS refer this

Upvotes: 0

Nasir
Nasir

Reputation: 531

Change mail port number from 465 to 587 and enable TLS.

spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=DERP
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Upvotes: 0

Related Questions