SMTP连接阅读欢迎消息

  • 本文关键字:消息 连接 SMTP 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

对标准库源的检查表明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)

最新更新