spring Sping Boot -无法连接到SMTP主机:smtp.gmail.com,端口:25,响应:421

ffscu2ro  于 2023-01-01  发布在  Spring
关注(0)|答案(4)|浏览(400)

我正在使用Gmail SMTP主机发送邮件与 Spring 启动和JavaMail发件人:
我的邮件属性:

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端口?

cigdeys3

cigdeys31#

我不知道你从哪里得到的这些属性。更常见的Sping 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;
}
6ioyuze2

6ioyuze22#

其实我发现什么地方出了问题,我应该使用这两个其中之一是我的服务器和其他的Gmail服务器的端口之一:

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

u4vypkhs3#

已在属性文件中禁用**mail.smtp.starttls.requiredfalse**。

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=false
sirbozc5

sirbozc54#

试试这个

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

确保谷歌允许不太安全的应用程序:https://myaccount.google.com/lesssecureapps打开

相关问题