首页 >后端开发 >Golang >发送电子邮件时出现错误 wsarecv:现有连接被远程主机强行关闭

发送电子邮件时出现错误 wsarecv:现有连接被远程主机强行关闭

PHPz
PHPz转载
2024-02-12 15:57:05912浏览

发送电子邮件时出现错误 wsarecv:现有连接被远程主机强行关闭

问题内容

我有下面的 go 程序,它会发送电子邮件。凭证是正确的。我什至用curl测试了它们,我发现连接是成功的。请注意,tls 不是必需的。

package main

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

const (
    username = "[email protected]"
    passwd   = "password1111"
    host     = "mail.privateemail.com"
    port     = "465"
)

func main() {
    from := "[email protected]"
    to := []string{
        "[email protected]",
    }
    msg := []byte("from: [email protected]\r\n" +
        "to: [email protected]" +
        "subject: golang testing mail\r\n" +
        "email body: welcome to go!\r\n")

    auth := smtp.plainauth("", username, passwd, host)
    url := fmt.sprintf(host + ":" + port)
    fmt.printf("url=[%s]\n", url)
    err := smtp.sendmail(url, auth, from, to, msg)
    if err != nil {
        log.fatal(err)
    }
    fmt.println("mail sent successfully!")
}

您能否告诉我为什么会出现以下错误?

read tcp 192.168.0.2:61740->198.54.122.135:465: wsarecv: an existing connection was forcibly closed by the remote host.
exit status 1

我尝试使用curl,我看到它连接到邮件服务器,但连接已关闭。

c:\GoProjects\goemail
λ curl -v --url "smtp://mail.privateemail.com:465" --user "[email protected]:password1111" --mail-from "[email protected]" --mail-rcpt "[email protected]" --upload-file sample.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 198.54.122.135:465...
* Connected to mail.privateemail.com (198.54.122.135) port 465 (#0)
  0     0    0     0    0     0      0      0 --:--:--  0:00:09 --:--:--     0* Recv failure: Connection was reset
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
* Closing connection 0
curl: (56) Recv failure: Connection was reset

我正在等待发送一封电子邮件。

解决方法

非常感谢您的回复。我从 https://www.php.cn/link/7104a226fe65be03fecf10f5bceff8a6 切换到实现 并且工作正常。我仍然不明白我做错了什么。我对 TLS 的看法是错误的 - 它被使用并且 go 方法也考虑到了它。

以上是发送电子邮件时出现错误 wsarecv:现有连接被远程主机强行关闭的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除