我尝试连接到 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)
关于go - SMTP 连接阅读欢迎信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49962763/