【问题标题】:Connecting to Exchange with Golang使用 Golang 连接到 Exchange
【发布时间】:2017-07-07 10:36:39
【问题描述】:

如何使用 Go 连接 Exchange 服务器?我试过了:

func main() {

to := "first.last@acme.com"
from := "me@acme.com"
password := "myKey"
subject := "Subject Here"
msg := "Message here"

emailTemplate := `To: %s
Subject: %s

%s
`
body := fmt.Sprintf(emailTemplate, to, subject, msg)
auth := smtp.PlainAuth("", from, password, "smtp.office365.com")
err := smtp.SendMail(
    "smtp.office365.com:587",
    auth,
    from,
    []string{to},
    []byte(body),
)
if err != nil {
    log.Fatal(err)
}
}

此代码返回:

504 5.7.4 Unrecognized authentication type

我正在移植 Python/Django 代码,它有一个设置,我标记必须声明:

EMAIL_USE_TLS = True

也许在 Go 中有类似的东西?

【问题讨论】:

标签: go smtp exchange-server


【解决方案1】:

Office 在 2017 年 8 月之后不支持 AUTH PLAIN。Ref。但是,它确实支持 AUTH LOGIN。 AUTH LOGIN 没有本地 Golang 实现,但这是一个可行的实现:

type loginAuth struct {
    username, password string
}

// LoginAuth is used for smtp login auth
func LoginAuth(username, password string) smtp.Auth {
    return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
    return "LOGIN", []byte(a.username), nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
    if more {
        switch string(fromServer) {
        case "Username:":
            return []byte(a.username), nil
        case "Password:":
            return []byte(a.password), nil
        default:
            return nil, errors.New("Unknown from server")
        }
    }
    return nil, nil
}

用 Golang 库中的 stmp.PlainAuth 替换此身份验证实现,它应该会相应地工作。

【讨论】:

    猜你喜欢
    • 2018-05-22
    • 2020-05-31
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多