使用sendgrid发送ssl电子邮件而没有完成

时间:2019-06-18 13:14:42

标签: go

我尝试使用ssl smtp从发送网格发送电子邮件,我当前的代码是:

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
}

问题是,当我运行代码时,电子邮件不发送,连接永不中断,不返回任何错误。 可以理解我哪里出了错吗?我想念什么? 没有ssl smtp即可正常工作。

0 个答案:

没有答案