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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。