使用Golang连接到交易所

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

Connecting to Exchange with Golang

问题

如何使用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中可能有类似的设置吗?

英文:

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

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)
}
}

This code returns:

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:

EMAIL_USE_TLS = True

Perhaps something similar in Go?

答案1

得分: 8

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

type loginAuth struct {
    username, password string
}

// LoginAuth用于smtp登录认证
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("未知的服务器返回")
        }
    }
    return nil, nil
}

将此身份验证实现替换为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:

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
}

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:

确定