Gmail SMTP timeout sending mail with Go net/smtp

Viewed 26

I'm trying to use gmail SMTP relay to send email with go net/smtp from a docker container. I keep getting this error dial tcp 142.251.12.108:587: connect: connection timed out.

I have enabled 2-step verification and set an app password and required TLS. I tried using the IP of my server but that didn't work either, whether TLS is required or not. The user is the only user in the Google Workspace account.

None of the suggested questions contains any solution that explains why my code isn't establishing a TLS connection or how to fix it. One suggested solution is Java which I don't understand and another suggests using "less secure" connections which is isn't available as an option anymore. I have spent many hours searching for a solution to this issue without success. The net/smtp package is apparently supposed to establish a TLS connection so I have no idea why it doesn't. I disagree with the assertion that programming language does not matter. Every package contains functions which differ in implementation somewhat. The solution must be a go code solution.

Here is my code

authName := "mark@example.com"

password := "secret"

from := "mark@example.com"

host := "smtp.gmail.com"

port := "587"

body := []byte(message)

auth := smtp.PlainAuth("", authName, password, host)

toList = append(toList, "sombody@somedomain.com")

err = smtp.SendMail(host+":"+port, auth, from, toList, body)

edit: The issue turned out to be that Linode block common SMTP ports by default. If you hit this error on a Linode, raise a ticket to explain why you need to send emails and what you will do to prevent spam. They will likely then lift the restriction.

0 Answers
Related