I try to send email using ssl smtp from send grid, my current code is:
import (
"crypto/tls"
"fmt"
"net"
"net/smtp"
"time"
"github.com/toorop/go-dkim"
"golang.org/x/net/proxy"
)
// SmtpSendOne - send email using smtp & proxy
func SmtpSendOne(server SmtpServer, data *MailData) error {
var remote net.Conn
var err error
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: "example.com",
}
remote, err = net.DialTimeout("tcp", "smtp.sendgrid.net:456", 10*time.Second)
defer remote.Close()
if remote == nil {
return fmt.Errorf("Proxy not working")
}
if err != nil {
return fmt.Errorf("Failed to connect to SMTP server: %s", err)
}
client, err := smtp.NewClient(remote, server.Host)
if err != nil {
return err
}
defer client.Close()
client.StartTLS(tlsconfig)
auth := SmtpLoginAuth("apikey", "apikey")
authErr := client.Auth(auth)
if authErr != nil {
return authErr
}
if err = client.Mail(data.From()); err != nil {
return fmt.Errorf("MAIL failed : %s", err)
}
if err = client.Rcpt(data.To()); err != nil {
return fmt.Errorf("RCPT failed %s", err)
}
bodyWriter, err := client.Data()
if err != nil {
return err
}
bodyWriter.Close()
client.Quit()
return nil
}
The problem is, when I run the code, email not send, and connection never dies, not return any error. Same one can understand where I went wrong? What am I missing? with no ssl smtp is working without any problem.