【问题标题】:Send email through unencrypted connection通过未加密的连接发送电子邮件
【发布时间】:2012-06-19 10:09:24
【问题描述】:

我有一个不使用加密连接的 SMTP 帐户。我可以使用同一个帐户从 C# 和 Python 发送电子邮件而不会出现问题,但使用 Go 时出现错误: 未加密的连接

这是我正在使用的代码:

package main

import (
        "log"
        "net/smtp"
)

func main() {
        // Set up authentication information.
        auth := smtp.PlainAuth(
                "",
                "user@example.com",
                "password",
                "mail.example.com",
        )
        // Connect to the server, authenticate, set the sender and recipient,
        // and send the email all in one step.
        err := smtp.SendMail(
                "mail.example.com:25",
                auth,
                "sender@example.org",
                []string{"recipient@example.net"},
                []byte("This is the email body."),
        )
        if err != nil {
                log.Fatal(err)
        }
}

【问题讨论】:

    标签: go smtp-auth


    【解决方案1】:

    这里的问题是smtp.PlainAuth 拒绝通过未加密的连接发送您的密码。这是为了保护你自己。像smtp.CRAMMD5Auth 这样的东西会是一个更好的选择。使用 CRAM-MD5 时,即使通过未加密的连接,您的密码也不会泄露。

    如果您仍然想使用普通身份验证,则需要制作自己的smtp.PlainAuth 版本。幸运的是,这是一件非常容易的事情。只需从标准库中复制 20 行左右并删除:

    if !server.TLS {
        return "", nil, errors.New("unencrypted connection")
    }
    

    http://golang.org/src/pkg/net/smtp/auth.go?s=1820:1882#L41 包含代码。

    如果您不想复制代码,您可以通过将函数返回的 smtp.Auth 包装在您自己的类型中来重用标准库实现。这样你拦截*smtp.ServerInfo 并欺骗实际的身份验证机制(来自标准库)存在加密连接。确保大量评论以明确您为什么要做您正在做的事情。像这样的东西(未经测试):

    type unencryptedAuth struct {
        smtp.Auth
    }
    
    func (a unencryptedAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
        s := *server
        s.TLS = true
        return a.Auth.Start(&s)
    }
    
    auth := unencryptedAuth {
        smtp.PlainAuth(
            "",
            "user@example.com",
            "password",
            "mail.example.com",
        ),
    }
    

    【讨论】:

    • 另一种硬编码方式:只需在文件系统的 net/smtp 库的源文件中注释这 3 行即可。
    • @Slavik 你如何部署它?
    • @ArchieYalakki 我从本地机器部署了编译库。它是我自己的需要。对于具有多个环境或多个贡献者的项目,不要在没有分叉的情况下编辑其他库 =)
    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2016-04-08
    • 2011-12-19
    • 2020-01-06
    • 2014-05-23
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多