go gomail.v2发送邮件报错unencrypted connection

发布时间 2023-10-10 10:07:01作者: 刘东才

实现Auth接口

type auth struct {
    host     string
    username string
    password string
}

func (a *auth) Start(server *smtp.ServerInfo) (proto string, toServer []byte, err error) {
    if !server.TLS {
        advertised := false
        for _, mechanism := range server.Auth {
            if mechanism == "LOGIN" {
                advertised = true
                break
            }
        }
        if !advertised {
            return "", nil, errors.New("gomail: unencrypted connection")
        }
    }
    //if server.Name != a.host {
    //    return "", nil, errors.New("gomail: wrong host name")
    //}
    return "LOGIN", nil, nil
}
func (a *auth) Next(fromServer []byte, more bool) (toServer []byte, err error) {
    if !more {
        return nil, nil
    }

    switch {
    case bytes.Equal(fromServer, []byte("Username:")):
        return []byte(a.username), nil
    case bytes.Equal(fromServer, []byte("Password:")):
        return []byte(a.password), nil
    default:
        return nil, fmt.Errorf("gomail: unexpected server challenge: %s", fromServer)
    }
}

 

 

使用auth

dialer.Auth = &auth{
        username: username,
        password: password,
        host:     host,
    }