go-smtp,无法通过 gmail 发送电子邮件,收到 EOF

标签 go gmail

我正在使用 go-smtp 尝试向自己发送电子邮件: https://github.com/emersion/go-smtp

    auth := sasl.NewPlainClient("", USERNAME, PASSWORD)
    to := []string{USERNAME}

    msg := strings.NewReader("To: " + USERNAME + "\r\n" +
        "Subject: testing golang go-smtp!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")

    err := smtp.SendMail(SERVER_HOST + ":" + SERVER_PORT, auth, USERNAME, to, msg)
    if err != nil {
        log.Fatal(err)
    }

输出是: 结束符 退出状态1

我正在连接到 smtp.gmail.com:465 并且可以通过 thunderbird 发送电子邮件。

感谢您的帮助。

沃尔特

最佳答案

在此处查看“配置选项”部分 https://support.google.com/a/answer/176600?hl=en .它提到 Gmail 在端口 465 上对 SMTP 服务器使用 SSL,对端口 587 使用 TLS。要解决您的问题,您可以使用端口 587,因为 smtp.SendMail() 函数在内部调用 net.Dial(),它使用纯 TCP 发送邮件流量,并在此过程中稍后调用 STARTTLS。

如果您想使用端口 465,这里有一个很好的示例,可以使用 SSL 发送电子邮件。

https://gist.github.com/chrisgillis/10888032

上述链接中也提到了这两种方法之间的主要区别在于,在 SSL 中,TLS 连接是从一开始就建立的,而如果您使用端口 587,连接将通过没有加密的普通 TCP 开始。

关于go-smtp,无法通过 gmail 发送电子邮件,收到 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063411/

相关文章:

Ruby 1.9 的 Ruby Pony 替代品?

去安装。执行自定义操作

android - 寻找 Android Gmail SMTP Oauth 示例

multithreading - 从函数返回后让 goroutines 保持运行

linux - Golang : How to cross compile on Linux for Windows

php - 使用 Gmail API 获取 "From"和 "To"地址

c# - 无法从文本框传递变量

python - 来自 python smtplib 的 Gaierror

go - 在 Go 中使用简单 channel 的竞争条件?

postgresql - Golang SSH 隧道连接到远程 postgres 数据库