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

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

Go send email gives error wsarecv: An existing connection was forcibly closed by the remote host

问题

我有以下的Go程序,用于发送电子邮件。凭据是正确的。我甚至使用curl进行了测试,我看到连接是成功的。请注意,不需要TLS。

package main

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

const (
	USERNAME = "ryuken@email.com"
	PASSWD   = "password1111"
	HOST     = "mail.privateemail.com"
	PORT     = "465"
)

func main() {
	from := "ryuken@email.com"
	to := []string{
		"info@email.com",
	}
	msg := []byte("From: ryuken@email.com\r\n" +
		"To: info@email.com" +
		"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 "ryuken@email.com:password1111" --mail-from "ryuken@email.com" --mail-rcpt "info@email.com-" --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

我期望能成功发送一封电子邮件。

英文:

I have the below Go program which sends an email. The credentials are correct. I even tested them with curl and I see tha the connection is successsful. Please note that TLS is not required.

package main

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

const (
	USERNAME = "ryuken@email.com"
	PASSWD   = "password1111"
	HOST     = "mail.privateemail.com"
	PORT     = "465"
)

func main() {
	from := "ryuken@email.com"
	to := []string{
		"info@email.com",
	}
	msg := []byte("From: ryuken@email.com\r\n" +
		"To: info@email.com" +
		"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!")
}

Could you please let me know why I get the below error?
> 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

I tried using curl and I saw that it connects to the mail server but the the connection is closed.

c:\GoProjects\goemail
λ curl -v --url "smtp://mail.privateemail.com:465" --user "ryuken@email.com:password1111" --mail-from "ryuken@email.com" --mail-rcpt "info@email.com-" --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

I'm expecting an email to be sent.

答案1

得分: 0

根据错误信息Recv failure: Connection was reset,我有几个可能导致问题的想法。

这个错误提示表示服务器返回了一个RST包,导致连接立即断开。

换句话说,这可能是一个TCP问题,或者可能是你这边的防火墙配置错误?这个应用程序在哪里运行,有什么样的上下文/配置?

另外,你强调不需要使用TLS,但是你使用的是端口465,该端口通过TLS传输消息。这是有意为之吗?

英文:

Given the error Recv failure: Connection was reset I have a couple of things in mind which could potentially be your issue.

This response essentially says that the server returned an RST package back, which drops the connection immediately.

In other words this might be a TCP issue, or maybe a firewall misconfiguration from your end? Where is this app running and what kind of context/config is in place?

ps: you highlight that TLS is not required but you use port 465 which transmits messages via TLS. Is this intentional?

答案2

得分: 0

非常感谢您的回复。我已经切换到来自https://gist.github.com/chrisgillis/10888032的实现,它运行良好。我仍然不明白我之前做错了什么。我对TLS的理解是错误的-它被使用,并且go方法也将其考虑在内。

英文:

Many thanks for the responses. I switched to the implementation from https://gist.github.com/chrisgillis/10888032
and it is working fine. I still don't get what I was doing wrong. I was wrong about TLS - it is used and the go method also takes it into consideration.

huangapple
  • 本文由 发表于 2023年1月7日 23:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75041611.html
匿名

发表评论

匿名网友

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

确定