mk8*_*efz 5 exchange-server smtp go
如何使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
此代码返回:
504 5.7.4 Unrecognized authentication type
Run Code Online (Sandbox Code Playgroud)
我正在移植Python / Django代码,它具有一个设置,我必须在其中声明:
EMAIL_USE_TLS = True
Run Code Online (Sandbox Code Playgroud)
也许Go中有类似的东西?
2017 年 8 月之后,Office 不支持 AUTH PLAIN。参考。但是,它确实支持 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
}
Run Code Online (Sandbox Code Playgroud)
用stmp.PlainAuth来自 Golang 的 lib替换这个 auth 实现,它应该会相应地工作。
| 归档时间: |
|
| 查看次数: |
1865 次 |
| 最近记录: |