Reputation: 10449
I have following code for creating an SMTP connection:
func NewSMTPClient(host, username, password string, port int) (*smtp.Client, error) {
tlsConfig := &tls.Config{ServerName: host}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), tlsConfig)
if err != nil {
return nil, fmt.Errorf("smtp client: tcp dial: %s", err.Error())
}
c, err := smtp.NewClient(conn, host)
if err != nil {
return nil, fmt.Errorf("smtp client: new client: %s", err.Error())
}
auth := smtp.PlainAuth("", username, password, host)
if err = c.Auth(auth); err != nil {
return nil, fmt.Errorf("smtp client: get auth: %s", err.Error())
}
return &c, nil
}
This code works very well with Yandex
SMTP server. But when I tried to test it with MailTrap
in my unit tests (since I didn't want to actually send email to people every time I test the code), I got this error:
smtp client: tcp dial: tls: first record does not look like a TLS handshake
EDIT 1:
I have tried to set InsecureSkipVerify
to true
, but still got the same error; though the MailTrap
configuration says that I should be able to use TLS
:
TLS: Optional (STARTTLS on all ports)
Upvotes: 1
Views: 11521
Reputation: 866
Just adding an answer to this question in case it's still helpful
In this case, we must use net.Dial
instead of tls.Dial
. I had the same issue, which was resolved just by using net.Dial
. So, in this case
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
}
Upvotes: 1
Reputation: 50
Probably the SMTP server you are trying to connect to doesn't support STARTTLS.
Upvotes: 0