2013-10-11 158 views
3

我使用go的net/smtp發送電子郵件,它對一些電子郵件正常工作,但不適用於其他電子郵件。我得到一個554 5.5.1 Error: no valid recipients,但我很確定我給了正確的郵件地址。調試SMTP連接

(最終目標是讓所有郵件收件人net/smtp的工作。所以答案上亦歡迎)

如何調試發生了什麼事情的背景是什麼?什麼命令發送到SMTP服務器和從SMTP服務器發送?我想重播命令行(telnet)中的命令以瞭解更多關於錯誤的信息。

這是我使用的代碼(from the go-wiki):

package main 

import (
     "bytes" 
     "log" 
     "net/smtp" 
) 

func main() { 
     // Connect to the remote SMTP server. 
     c, err := smtp.Dial("mail.example.com:25") 
     if err != nil { 
       log.Fatal(err) 
     } 
     // Set the sender and recipient. 
     c.Mail("[email protected]") 
     c.Rcpt("[email protected]") 
     // Send the email body. 
     wc, err := c.Data() 
     if err != nil { 
       log.Fatal(err) 
     } 
     defer wc.Close() 
     buf := bytes.NewBufferString("This is the email body.") 
     if _, err = buf.WriteTo(wc); err != nil { 
       log.Fatal(err) 
     } 
} 

我知道這是一個非常模糊的問題,但任何幫助深表感謝。

+0

您是否可以將收件人複製到您可以發佈的內容,以便重現此行爲? – nemo

+1

@nemo我已經做了一些進一步的調試,看起來好像其他主機需要非本地主機'EHLO'命令。所以'client.Hello(myhost)'幫了個忙。我應該自問或者完全刪除它嗎? – topskip

+0

絕對自我回答!對於同一個人的任何人來說,這都是有價值的信息 – nemo

回答

3

它看起來(一些調試後),即誰拒絕了有效的電子郵件地址的服務器需要有一個適當的主機名EHLO命令,不只是localhost這是默認爲documented

所以,當我插入一行像這樣的開始

c.Hello("example.com") // use real server name here 

一切工作正常。 但是:(注意自己):如果沒有正確的錯誤檢查,永遠不會運行代碼。它幫助我檢查所有的命令如

err = c.Hello("example.com") 
... 
err = c.Mail("[email protected]") 
... 
err = c.Rcpt("[email protected]") 
... 

我不知道了其中的錯誤信息給我用正確的主機名提示的錯誤,但它是這樣的:

550 5.7.1 <localhost>: Helo command rejected: Don't use hostname localhost