登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-02-18 15:24:24 268浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《电子邮件发送错误:远程主机强行关闭现有连接》,聊聊,我们一起来看看吧!

问题内容

我有下面的 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://gist.github.com/chrisgillis/10888032 切换到实现 并且工作正常。我仍然不明白我做错了什么。我对 TLS 的看法是错误的 - 它被使用并且 go 方法也考虑到了它。

本篇关于《电子邮件发送错误:远程主机强行关闭现有连接》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>