golang gomail SMTP error: An existing connection was forced to be terminated by the remote host

162 Views Asked by At

I'm making a email send package using: "gopkg.in/gomail.v2" So I create a file and the call it Everything work fine. The config is loaded fine but when i tried to send the email i get this erro: "An existing connection was forced to be terminated by the remote host"

I read some problems from others and reed about changes in the TLSConfig so i made it. But still doesn't work.

func ConnectSmtpServer(smtpConfig SmtpConfig) (gomail.Dialer, SmtpConfig) {
    // load en .env config
    smtp_config := LoadConfig()

    // connect to smtp server
    smtp_server := gomail.NewDialer(smtp_config.Host, smtp_config.Port, smtp_config.Username, smtp_config.Password)
    smtp_server.TLSConfig = &tls.Config{
        InsecureSkipVerify: true,
        // Configuracion de cifrado
        CipherSuites: []uint16{
            tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
            tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
        },
        PreferServerCipherSuites: true,
        MinVersion:               tls.VersionTLS11,
        MaxVersion:               tls.VersionTLS11, 
    }
    return *smtp_server, smtp_config
}

func SendEmail(to string, subject string, body string, from string, server gomail.Dialer) {
    // prepare email
    new_email := gomail.NewMessage()
    new_email.SetHeader("From", from)
    new_email.SetHeader("To", to)
    new_email.SetHeader("Subject", subject)
    new_email.SetBody("text/html", body)
    
    // send email
    if err := server.DialAndSend(new_email); err != nil {
        // print more information about the error
        fmt.Printf("%s", err)
        panic(err)
    
    } else {
        fmt.Println("Email enviado a: " + to)
    }
}
0

There are 0 best solutions below