email-outlook-auth.go 909 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package common
  2. import (
  3. "errors"
  4. "net/smtp"
  5. "strings"
  6. )
  7. type outlookAuth struct {
  8. username, password string
  9. }
  10. func LoginAuth(username, password string) smtp.Auth {
  11. return &outlookAuth{username, password}
  12. }
  13. func (a *outlookAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) {
  14. return "LOGIN", []byte{}, nil
  15. }
  16. func (a *outlookAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  17. if more {
  18. switch string(fromServer) {
  19. case "Username:":
  20. return []byte(a.username), nil
  21. case "Password:":
  22. return []byte(a.password), nil
  23. default:
  24. return nil, errors.New("unknown fromServer")
  25. }
  26. }
  27. return nil, nil
  28. }
  29. func isOutlookServer(server string) bool {
  30. // 兼容多地区的outlook邮箱和ofb邮箱
  31. // 其实应该加一个Option来区分是否用LOGIN的方式登录
  32. // 先临时兼容一下
  33. return strings.Contains(server, "outlook") || strings.Contains(server, "onmicrosoft")
  34. }