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

Time:01-09

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

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 "[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 expecting an email to be sent.

CodePudding user response:

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?

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related