1

我有以下用于创建 SMTP 连接的代码:

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
}

此代码在YandexSMTP 服务器上运行良好。但是当我尝试MailTrap在我的单元测试中测试它时(因为我不想每次测试代码时都向人们发送电子邮件),我收到了这个错误:

smtp client: tcp dial: tls: first record does not look like a TLS handshake

编辑1:

我尝试设置InsecureSkipVerifytrue,但仍然出现相同的错误;虽然MailTrap配置说我应该能够使用TLS

TLS: Optional (STARTTLS on all ports)
4

1 回答 1

1

您尝试连接的 SMTP 服务器可能不支持 STARTTLS。

于 2020-07-02T14:28:43.427 回答