Spring Boot - 无法连接到 SMTP 主机:smtp.gmail.com,端口:25,响应:421
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28064904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421
提问by Amar AttilaZz
I'm using gmail smtp host t send mails with spring boot and JavaMail Sender :
我正在使用 gmail smtp 主机发送带有 spring boot 和 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
Geting 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 ?
即使我使用的是 465 端口,为什么他指向端口 25 ?
采纳答案by Amar AttilaZz
Actually I found what going wrong, I should use both one of them is the port of my server and the other the one of gmail server :
实际上我发现出了什么问题,我应该同时使用其中一个是我的服务器的端口,另一个是 gmail 服务器的端口:
spring.mail.properties.mail.smtp.socketFactory.port = 25
mail.smtp.port= 465
回答by Steve
I'm not sure where you got those properties. The more common Spring Boot properties to configure can be found here:
我不确定你从哪里得到这些属性。可以在此处找到要配置的更常见的 Spring Boot 属性:
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
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.mailnamespace are:
所以你可能应该使用spring.mail.port. spring.mail命名空间中可用的属性是:
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 JavaMailSenderas a bean like so:
但是,如果您要创建自己的JavaMailSender,则设置 SMTP 端口的属性为mail.smtp.port。我将它设置JavaMailSender为一个 bean,如下所示:
@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;
}
回答by Ajay Kumar
disabled mail.smtp.starttls.requiredto falsein your properties file.
在您的属性文件中将mail.smtp.starttls.required禁用为false。
spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=false
spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=false
回答by kishor Bhosale
Try this
尝试这个
spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.username = xxxxxx
spring.mail.password = xxxxxx
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.starttls.required = true
spring.mail.properties.mail.smtp.auth = true
Make sure google allow less secure app: https://myaccount.google.com/lesssecureappsturn it on
确保谷歌允许不太安全的应用程序:https: //myaccount.google.com/lesssecureapps打开它

