调试SMTP连接

huangapple go评论82阅读模式
英文:

Debugging SMTP connection

问题

我使用Go语言的net/smtp包发送电子邮件,对于某些邮件来说它工作得很好,但对于其他邮件则不行。我收到一个554 5.5.1 Error: no valid recipients的错误,但我非常确定我提供了正确的邮件地址。

(最终目标是使net/smtp适用于所有的邮件收件人。所以对此方面的答案也是可以的)

我该如何调试后台发生的情况?有哪些命令被发送到SMTP服务器并从服务器返回?我想从命令行(telnet)中重放这些命令,以了解更多关于错误的信息。

这是我使用的代码(来自go-wiki):

package main

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

func main() {
        // 连接到远程SMTP服务器。
        c, err := smtp.Dial("mail.example.com:25")
        if err != nil {
                log.Fatal(err)
        }
        // 设置发件人和收件人。
        c.Mail("sender@example.org")
        c.Rcpt("recipient@example.net")
        // 发送邮件正文。
        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)
        }
}

我知道这是一个非常模糊的问题,但任何帮助都将不胜感激。

英文:

I use go's net/smtp to send emails, and it works fine for some emails but not for others. I get an 554 5.5.1 Error: no valid recipients but I am pretty sure I give the correct mail address.

(The ultimate goal is to make the net/smtp work for all mail recipients. So answers on that are welcome as well)

How can I debug what's going on in the background? What commands are sent to and from the SMTP server? I'd like to replay the commands from the command line (telnet) to get find out more about the error.

This is the code I use (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("sender@example.org")
        c.Rcpt("recipient@example.net")
        // 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)
        }
}

I know this is a very vague question, but any help is much appreciated.

答案1

得分: 4

经过一些调试,看起来拒绝有效电子邮件地址的服务器需要一个带有正确主机名的EHLO命令,而不仅仅是默认的localhost,这在文档中有记录。

所以当我在开头插入这样一行代码时:

c.Hello("example.com") // 在这里使用真实的服务器名称

一切都正常工作。但是(对自己的备注):永远不要在没有适当错误检查的情况下运行代码。这帮助我检查了所有命令的错误,例如:

err = c.Hello("example.com")
...
err = c.Mail("sender@example.org")
...
err = c.Rcpt("recipient@example.net")
...

我不确定哪个错误消息给了我使用正确主机名的提示,但它大致是这样的:

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

It looks (after some debugging), that the servers who rejected the valid email addresses require a EHLO command with a proper hostname, not just localhost which is the default as documented

So when I insert a line in the beginning like this

c.Hello(&quot;example.com&quot;) // use real server name here

everything works fine. BUT: (note to myself): never run code without proper error checking. It helped me to check the error on all the commands such as

err = c.Hello(&quot;example.com&quot;)
...
err = c.Mail(&quot;sender@example.org&quot;)
...
err = c.Rcpt(&quot;recipient@example.net&quot;)
...

I am not sure anymore which error message gave me the hint to use a proper hostname, but it was something like:

550 5.7.1 &lt;localhost&gt;: Helo command rejected: Don&#39;t use hostname localhost

huangapple
  • 本文由 发表于 2013年10月11日 19:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/19317408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定