SMTP连接读取欢迎消息

时间:2018-04-22 05:21:49

标签: go smtp

我尝试连接smtp服务器并阅读欢迎消息。这是我的代码:

package main

import (
    "fmt"
    "net"
    "time"
    "net/smtp"
    "bufio"
)

func main() {
    // attempt a connection
    conn, _ := net.DialTimeout("tcp", "88.198.24.108:25", 15 * time.Second)

    buf := bufio.NewReader(conn)
    bytes, _ := buf.ReadBytes('\n')
    fmt.Printf("%s", bytes)


    client, err := smtp.NewClient(conn, "88.198.24.108")
    if err != nil {
        fmt.Println("1>>", err)
        return
    }

    client.Quit()
    conn.Close()
}

问题是在读取欢迎消息停止运行并等待超时后,我想阅读/打印欢迎消息并继续。

220 example.me ESMTP Haraka/2.8.18 ready
1>> 421 timeout

1 个答案:

答案 0 :(得分:1)

对标准库源的检查表明smtp.NewClient()从远程主机读取SMTP标题并将其丢弃。

func NewClient(conn net.Conn, host string) (*Client, error) {
    text := textproto.NewConn(conn)
    _, _, err := text.ReadResponse(220)
    if err != nil {
        text.Close()
        return nil, err
    }
    c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
    _, c.tls = conn.(*tls.Conn)
    return c, nil
}

您想要阅读此横幅并决定是否根据其内容发送邮件。

由于您已经自己阅读了横幅,并且可能会对此做出决定,而不是调用smtp.NewClient(),您应该在自己的代码中实现NewClient()的其余部分,可能是这样的:

    client := &smtp.Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
    _, client.tls = conn.(*tls.Conn)
相关问题