i set port 587 as recomended in my application.propertys, but springboot seems to use port 25 still despite, can someone tell me why port 25 don't works, or more important how i fix my smtp port 587? thanks in advance!

error:

2023-12-18T15:30:19.084+01:00 ERROR 15800 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exceptions (1) are:
Failed message 1: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect] with root cause

java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
    at java.base/sun.nio.ch.Net.connect(Net.java:579) ~[na:na]
    at java.base/sun.nio.ch.Net.connect(Net.java:568) ~[na:na]
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:585) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:583) ~[na:na]
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:361) ~[jakarta.mail-1.1.0.jar:na]
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236) ~[jakarta.mail-1.1.0.jar:na]
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209) ~[jakarta.mail-1.1.0.jar:na]
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:754) ~[jakarta.mail-1.1.0.jar:na]
    at jakarta.mail.Service.connect(Service.java:342) ~[jakarta.mail-1.1.0.jar:na]
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518) ~[spring-context-support-6.0.12.jar:6.0.12]
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437) ~[spring-context-support-6.0.12.jar:6.0.12]
    

my code:

@Component
public class EmailSender {


    @Bean
    public static JavaMailSenderImpl javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setProtocol("SMTP");
        javaMailSender.setHost("smtp.office365.com");
        javaMailSender.setPort(587);

        return javaMailSender;
    }

     JavaMailSender mailSender = new JavaMailSenderImpl();

    public void sendEmail(String to, String subject, String message){

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(message);

        mailSender.send(mailMessage);
    }

}

my application.propertys:

# mail
#spring.mail.host=smtp-mail.outlook.com
spring.mail.host=smtp.office365.com
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.port=587
spring.mail.username=${[email protected]}
spring.mail.password=${*secret*}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.debug=true

i tried to send e-mail in my application but get an error at port 25 while i have set smtp port 587

greets

1

There are 1 best solutions below

0
On

https://stackoverflow.com/a/38629240/23120849

This is craftwork, excellent! it works.

of course with port and host for outlook:

properties.put("mail.smtp.host", "smtp.office365.com");

properties.put("mail.smtp.port", 587);