2015-01-21 74 views
2

我使用Gmail SMTP宿主T發送郵件彈簧引導和JavaMail的發件人:春季啓動 - 無法連接到SMTP主機:smtp.gmail.com,端口:25,響應:421

我的郵件屬性:

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 

葛亭錯誤:

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

即使我使用端口465爲什麼他指着25端口?

+0

添加'spring.mail.port = 465'來改變港口。如果這不起作用,請添加您收到的錯誤。 – 2015-01-21 13:55:42

回答

1

其實我發現了什麼會錯了,我應該用他們兩個一個是我的服務器的端口和Gmail服務器的其他的一個:

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

我對Spring Boot一無所知,但通常你應該[永遠不需要設置套接字工廠屬性](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes) 。另請參閱[有關使用Gmail的一般JavaMail說明](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail)。 – 2015-01-21 20:13:32

2

我不確定你在哪裏得到這些屬性。較常見的春季啓動性能配置可以在這裏找到:

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

所以,你或許應該使用spring.mail.port。在spring.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; 
} 
+0

我試着將屬性spring.mail.port = 587添加到我的文件屬性 ,現在它指向正確的端口,但仍然產生相同的錯誤,但具有正確的端口 失敗的消息1:javax.mail .MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:587; – 2015-01-21 12:07:37

+0

相同的錯誤代碼? – Steve 2015-01-21 12:15:47

+0

@Steve Spring Boot支持通過指定'application.properties'中的屬性來自動配置'JavaMailSenderImpl'。請參閱參考指南的[mail](http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-email)部分和[屬性部分] (http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#common-application-properties)。 – 2015-01-21 13:54:45

2

已禁用mail.smtp.starttls.requiredfalse在您的屬性文件中。

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

+0

謝謝,因爲我添加了「spring.mail.properties.mail.smtp.starttls.enable」,它對我來說工作正常。順便說一下,我無法在http://docs.spring中找到這些屬性。IO /彈簧引導/文檔/電流/參考/ HTML /共應用properties.html。 有什麼想法? – Dickson 2016-10-15 09:47:23

+0

spring.mail.properties是在spring-boot-autoconfigure中添加的前綴,但「mail.smtp.starttls.enable」是Java郵件API中的原始屬性名稱。 – Ajay 2016-10-17 03:54:08

+0

寒意..謝謝@Ajay的信息。 – Dickson 2016-10-23 22:43:15

相關問題