SMTP连接读取欢迎消息 [英] SMTP connection read welcome message

查看:162
本文介绍了SMTP连接读取欢迎消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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('\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()
}

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

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


推荐答案

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.

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

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)

这篇关于SMTP连接读取欢迎消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆