Home  >  Article  >  Backend Development  >  Error while sending email wsarecv: The existing connection was forcibly closed by the remote host

Error while sending email wsarecv: The existing connection was forcibly closed by the remote host

PHPz
PHPzforward
2024-02-12 15:57:05894browse

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

Question content

I have the following go program which sends emails. The credentials are correct. I even tested them with curl and I found that the connection is successful. Note that tls is not required.

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!")
}

Can you tell me why the following error occurs?

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 it connected to the mail server but the connection was closed.

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

I'm waiting for an email to be sent.

Solution

Thank you very much for your reply. I switched to the implementation from https://www.php.cn/link/7104a226fe65be03fecf10f5bceff8a6 and works fine. I still don't understand what I did wrong. I was wrong about TLS - it is used and the go method takes it into account.

The above is the detailed content of Error while sending email wsarecv: The existing connection was forcibly closed by the remote host. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete