Spring Boot - 无法连接到SMTP主机:smtp.gmail.com,port:25,响应:421

时间:2015-01-21 10:32:12

标签: spring email smtp javamail spring-boot

我正在使用gmail smtp host t发送带有spring boot和JavaMail Sender的邮件:

我的邮件属性:

 spring.mail.host = smtp.gmail.com
 spring.mail.username = XXX@gmail.com
 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

获取错误:

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

即使我使用端口465,为什么他指向端口25?

4 个答案:

答案 0 :(得分:3)

在您的属性文件中禁用 mail.smtp.starttls.required false

spring.mail.properties.mail.smtp.starttls.enable =真 spring.mail.properties.mail.smtp.starttls.required =假

答案 1 :(得分:2)

我不知道你在哪里获得这些房产。可以在此处找到要配置的更常见的Spring Boot属性:

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

所以你应该使用spring.mail.portspring.mail命名空间中可用的属性为:

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

但是,如果您要创建自己的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;
}

答案 2 :(得分:2)

其实我发现出了什么问题,我应该使用其中一个是我服务器的端口而另一个是gmail服务器的端口:

spring.mail.properties.mail.smtp.socketFactory.port = 25
mail.smtp.port= 465

答案 3 :(得分:0)

尝试

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

确保Google允许安全性较低的应用程序: https://myaccount.google.com/lesssecureapps 开启