I try to connect on smtp server and read welcome message. This is my code:
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('
')
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()
}
Problem is after read welcome message stop running and wait to go in timeout, I want to read/print welcome message and continue.
220 example.me ESMTP Haraka/2.8.18 ready
1>> 421 timeout
An inspection of the standard library source indicates that smtp.NewClient()
reads the SMTP banner from the remote host and throws it away.
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
}
You want to read this banner and decide whether to send mail based on its contents.
Since you have already read the banner yourself, and presumably will make a decision on that, instead of calling smtp.NewClient()
you should then implement the rest of NewClient()
in your own code, possibly something like this:
client := &smtp.Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
_, client.tls = conn.(*tls.Conn)