使用Golang连接到交易所

huangapple go评论119阅读模式
英文:

Connecting to Exchange with Golang

问题

如何使用Go连接到Exchange服务器?我已经尝试了以下代码:

  1. func main() {
  2. to := "first.last@acme.com"
  3. from := "me@acme.com"
  4. password := "myKey"
  5. subject := "Subject Here"
  6. msg := "Message here"
  7. emailTemplate := `To: %s
  8. Subject: %s
  9. %s
  10. `
  11. body := fmt.Sprintf(emailTemplate, to, subject, msg)
  12. auth := smtp.PlainAuth("", from, password, "smtp.office365.com")
  13. err := smtp.SendMail(
  14. "smtp.office365.com:587",
  15. auth,
  16. from,
  17. []string{to},
  18. []byte(body),
  19. )
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. }

这段代码返回以下错误信息:

  1. 504 5.7.4 Unrecognized authentication type

我正在将Python/Django代码移植过来,它有一个设置项需要声明:

  1. EMAIL_USE_TLS = True

在Go中可能有类似的设置吗?

英文:

How do I connect with Exchange server with Go? I have tried:

  1. func main() {
  2. to := "first.last@acme.com"
  3. from := "me@acme.com"
  4. password := "myKey"
  5. subject := "Subject Here"
  6. msg := "Message here"
  7. emailTemplate := `To: %s
  8. Subject: %s
  9. %s
  10. `
  11. body := fmt.Sprintf(emailTemplate, to, subject, msg)
  12. auth := smtp.PlainAuth("", from, password, "smtp.office365.com")
  13. err := smtp.SendMail(
  14. "smtp.office365.com:587",
  15. auth,
  16. from,
  17. []string{to},
  18. []byte(body),
  19. )
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. }

This code returns:

  1. 504 5.7.4 Unrecognized authentication type

I'm porting over Python/Django code, and it has a setting where I mark have to declare:

  1. EMAIL_USE_TLS = True

Perhaps something similar in Go?

答案1

得分: 8

Office在2017年8月之后不再支持AUTH PLAIN身份验证。参考。但是,它支持AUTH LOGIN。AUTH LOGIN没有原生的Golang实现,但下面是一个可用的实现:

  1. type loginAuth struct {
  2. username, password string
  3. }
  4. // LoginAuth用于smtp登录认证
  5. func LoginAuth(username, password string) smtp.Auth {
  6. return &loginAuth{username, password}
  7. }
  8. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  9. return "LOGIN", []byte(a.username), nil
  10. }
  11. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  12. if more {
  13. switch string(fromServer) {
  14. case "Username:":
  15. return []byte(a.username), nil
  16. case "Password:":
  17. return []byte(a.password), nil
  18. default:
  19. return nil, errors.New("未知的服务器返回")
  20. }
  21. }
  22. return nil, nil
  23. }

将此身份验证实现替换为Golang库中的stmp.PlainAuth,应该可以正常工作。

英文:

Office does not support AUTH PLAIN after August 2017. Ref. However, it does support AUTH LOGIN. AUTH LOGIN does not have a native Golang implementation but here is a working one:

  1. type loginAuth struct {
  2. username, password string
  3. }
  4. // LoginAuth is used for smtp login auth
  5. func LoginAuth(username, password string) smtp.Auth {
  6. return &loginAuth{username, password}
  7. }
  8. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  9. return "LOGIN", []byte(a.username), nil
  10. }
  11. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  12. if more {
  13. switch string(fromServer) {
  14. case "Username:":
  15. return []byte(a.username), nil
  16. case "Password:":
  17. return []byte(a.password), nil
  18. default:
  19. return nil, errors.New("Unknown from server")
  20. }
  21. }
  22. return nil, nil
  23. }

Replace this auth implementation with the stmp.PlainAuth from Golang lib and it should work accordingly.

huangapple
  • 本文由 发表于 2017年2月18日 03:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/42305763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定