douqiu0796 2018-08-15 09:21
浏览 65
已采纳

Golang的smtp客户端的自定义拨号程序?

I'm try connect to smtp server via socks5 proxy

package main

import (
    "net"
    "net/smtp"

    "golang.org/x/net/proxy"
)

func main() { Connect() }
func Connect() {
    dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:465")
    if err != nil {
        panic(err)
    }
    client, err := smtp.NewClient(dialer, "smtp.gmail.com:465")
    if err != nil {
        panic(err)
    }
    auth := smtp.PlainAuth("", "mymailaddr@gmail.com", "", "smtp.gmail.com:465")
    if err = client.Auth(auth); err != nil {
        panic(err)
    }
}

func Socks(socks, addr string) (r net.Conn, err error) {
    Dial, err := proxy.SOCKS5("tcp", socks, nil, proxy.Direct)
    r, err = Dial.Dial("tcp", addr)
    return
}

And can't it, have error

panic: EOF

goroutine 1 [running]:
main.Connect()
        main.go:18 +0x1e5
main.main()
        main.go:10 +0x20
exit status 2

Does smtp.Client can any method to connect smtp server with socks proxy? I'm not found answer in Google and not found any library provide this features.

  • 写回答

1条回答 默认 最新

  • doujia7517 2018-08-15 10:14
    关注

    You are using port 465 which expects TLS from start (implicit TLS) and not the usual TLS after STARTTLS command (explicit TLS). This means that the Conn object you use as dialer should already have been upgraded to TLS. To do this:

    import "crypto/tls"
    ...
    func Connect() {
        dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:465")
        ...
        conf := &tls.Config{ServerName: "smtp.gmail.com"}
        tlsdialer := tls.Client(dialer, conf)
        client, err := smtp.NewClient(tlsdialer, "smtp.gmail.com:465")
    

    Alternatively you could use port 587 which requires explicit TLS:

    func Connect() {
        dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:587")
        ...
        conf := &tls.Config{ServerName: "smtp.gmail.com"}
        err = client.StartTLS(conf)
        ...
        auth := smtp.PlainAuth("", "mymailaddr@gmail.com", "", "smtp.gmail.com:587")
        if err = client.Auth(auth); err != nil {
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作