Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421

I'm using Gmail SMTP host to send mails with spring boot and JavaMail Sender:

my Mail properties:

spring.mail.host = smtp.gmail.com
spring.mail.username = [email protected]
spring.mail.password = XXX

spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false

Getting error:

Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.9business.fr, port: 25, response: 421] with root cause

even if I'm using port 465 why is he pointing to port 25?

like image 403
Amar AttilaZz Avatar asked Feb 12 '23 03:02

Amar AttilaZz


1 Answers

I'm not sure where you got those properties. The more common Spring Boot properties to configure can be found here:

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

So you should probably be using spring.mail.port. The properties available in the spring.mail namespace are:

host
port
username
password
defaultEncoding (default: "UTF-8")

However, if you are creating your own JavaMailSender, the property to set the SMTP port is mail.smtp.port. I set up the JavaMailSender as a bean like so:

@Value(value = "${mail.smtp.host}")
private String smtpHost;

@Value(value = "${mail.smtp.port}")
private String smtpPort;

@Bean
public JavaMailSender mailSender() {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();

    Properties p = new Properties();
    p.setProperty("mail.smtp.auth", "false");
    p.setProperty("mail.smtp.host", smtpHost);
    p.setProperty("mail.smtp.port", smtpPort);
    sender.setJavaMailProperties(p);

    return sender;
}
like image 109
Steve Avatar answered Feb 13 '23 23:02

Steve