如何检查 SMTP 是否在线并正在运行?
func (p *sys_ping) smtp(host string) string {
client, err := smtp.Dial(host)
if err != nil {
time.Sleep(time.Duration(p.delay) * time.Second)
return ""
}
// check if response code is 220 or something similar to check if server is running
client.Quit()
return host
}
上面的代码是连接SMTP。有用。但是如何检查响应代码是否为
220
?我猜正确的代码应该是220
来检查服务器是否运行正确?
是的,服务器应该响应代码 220 才能接受连接
import (
"net/smtp"
"time"
"strings"
)
func (p *sys_ping) smtp(host string) string {
client, err := smtp.Dial(host)
if err != nil {
time.Sleep(time.Duration(p.delay) * time.Second)
return ""
}
defer client.Quit()
// Read initial resp code from server
response, err := client.Text.ReadResponse(220)
if err != nil || !strings.HasPrefix(response, "220") {
return ""
}
return host
}