使用 TcpClient 连接并向 smtp.gmail.com 发送命令

发布于 2024-10-22 16:21:06 字数 4656 浏览 2 评论 0原文

我无法通过 smtp.gmail.com 发送任何命令。

当我使用 465 作为端口号连接时,我可以看到一些混合的字符。

如果我使用任何其他端口号(例如 25 或 587),则会失败并显示“由于意外的数据包格式,握手失败”。

这是我的代码..

 public delegate void StatusUpdateEvents(object o, clsStatusEventHandler e);
    public event StatusUpdateEvents statusupdate;
    clsStatusEventHandler objStatus = new clsStatusEventHandler();
    public bool EnableSSL { get; set; }
    public string strIpAddress { get; set; }
    public int portNo { get; set; }

    static public string EncodeTo64(string toEncode)
    {

        byte[] toEncodeAsBytes

              = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

        string returnValue

              = System.Convert.ToBase64String(toEncodeAsBytes);

        return returnValue;

    }



    public TcpClient GetTcpClient()
    {
      if(smtpTcpClient==null) smtpTcpClient = new TcpClient();

      return smtpTcpClient;
    }
    public TcpClient ConnectTcpClient()
    {
        if (smtpTcpClient.Connected) smtpTcpClient.Close();
        smtpTcpClient.Connect(strIpAddress, portNo);
        return smtpTcpClient;
    }
  public  void UpdateStatus(string strStatus)
    {
       objStatus.strStatus = strStatus;
     if (statusupdate!=null) statusupdate(this, objStatus);
    }



    bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {

        return true;

    }

    public StreamReader GetReadStream()
    {
        if (EnableSSL == true)
        {
            smtpNetworkStream = smtpTcpClient.GetStream();

            if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(GetSslStream()); }
            return smtpStreamReader;
        }
        else
        {
            smtpNetworkStream = smtpTcpClient.GetStream();
            if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(smtpNetworkStream); }
            return smtpStreamReader;
        }
    }

    public StreamWriter GetWriteStream()
    {
        if (EnableSSL == true)
        {
            smtpNetworkStream = smtpTcpClient.GetStream();

            //  readerSmtp = new StreamReader(new System.Net.Security.SslStream(clientSmtp.GetStream(), true, CertificateValidationCallback));

            if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(GetSslStream()); }

            return smtpStreamWriter;
        }
        else
        {
            smtpNetworkStream = smtpTcpClient.GetStream();
            if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(smtpNetworkStream); }

            return smtpStreamWriter;
        }

    }

    SslStream GetSslStream()
    {
        if (sslstream == null) { 
            sslstream = new SslStream(smtpNetworkStream);
           sslstream.AuthenticateAsClient(strIpAddress, null, System.Security.Authentication.SslProtocols.Tls, false);

        }
        return sslstream;
    }

    public void DoConnect(string strIpAddress, int portno,bool EnableSSL) 
    {
        this.strIpAddress = strIpAddress;
        this.portNo = portno;
        try
        {
            UpdateStatus("trying to connect to " + strIpAddress + ":" + portno.ToString());
            smtpTcpClient = GetTcpClient();
            smtpTcpClient.Connect(strIpAddress, portno);

            if(EnableSSL==true)
            {
                this.EnableSSL = EnableSSL;
                smtpNetworkStream = smtpTcpClient.GetStream();

               //sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("EHLO [email protected] \n"));
               //sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("STARTTLS \n"));

               smtpStreamReader = GetReadStream();//new StreamReader(new System.Net.Security.SslStream(smtpTcpClient.GetStream(), true,CertificateValidationCallback));
               //readerSmtp = new StreamReader(sslstream);
            }
            else
            {
                EnableSSL = false;
                smtpNetworkStream = smtpTcpClient.GetStream();
                smtpStreamReader = GetReadStream();
             }

            UpdateStatus(readResponse());

        }

        catch (Exception e)
        {
            //MessageBox.Show(e.Message);
            UpdateStatus("****CONNECTION ERROR****" + "\n" + e.Message);
            smtpTcpClient.Close();
        }

    }

请帮助我.. 非常感谢。.

编辑:我了解 System.net.mail.smtpclient 和其他可用的开源 smtp 库。但是,我无法使用它来手动发送 smtp 命令。这不是为了发送电子邮件。这是为了了解电子邮件协议通过 SSL 逐步工作。

I am unable to send any command through smtp.gmail.com.

I can see some mixed up characters when I connect with 465 as the port number.

If I use any other port number like 25 or 587, it fails with "The handshake failed due to an unexpected packet format."

Here's my code..

 public delegate void StatusUpdateEvents(object o, clsStatusEventHandler e);
    public event StatusUpdateEvents statusupdate;
    clsStatusEventHandler objStatus = new clsStatusEventHandler();
    public bool EnableSSL { get; set; }
    public string strIpAddress { get; set; }
    public int portNo { get; set; }

    static public string EncodeTo64(string toEncode)
    {

        byte[] toEncodeAsBytes

              = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

        string returnValue

              = System.Convert.ToBase64String(toEncodeAsBytes);

        return returnValue;

    }



    public TcpClient GetTcpClient()
    {
      if(smtpTcpClient==null) smtpTcpClient = new TcpClient();

      return smtpTcpClient;
    }
    public TcpClient ConnectTcpClient()
    {
        if (smtpTcpClient.Connected) smtpTcpClient.Close();
        smtpTcpClient.Connect(strIpAddress, portNo);
        return smtpTcpClient;
    }
  public  void UpdateStatus(string strStatus)
    {
       objStatus.strStatus = strStatus;
     if (statusupdate!=null) statusupdate(this, objStatus);
    }



    bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {

        return true;

    }

    public StreamReader GetReadStream()
    {
        if (EnableSSL == true)
        {
            smtpNetworkStream = smtpTcpClient.GetStream();

            if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(GetSslStream()); }
            return smtpStreamReader;
        }
        else
        {
            smtpNetworkStream = smtpTcpClient.GetStream();
            if (smtpStreamReader == null) { smtpStreamReader = new StreamReader(smtpNetworkStream); }
            return smtpStreamReader;
        }
    }

    public StreamWriter GetWriteStream()
    {
        if (EnableSSL == true)
        {
            smtpNetworkStream = smtpTcpClient.GetStream();

            //  readerSmtp = new StreamReader(new System.Net.Security.SslStream(clientSmtp.GetStream(), true, CertificateValidationCallback));

            if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(GetSslStream()); }

            return smtpStreamWriter;
        }
        else
        {
            smtpNetworkStream = smtpTcpClient.GetStream();
            if (smtpStreamWriter == null) { smtpStreamWriter = new StreamWriter(smtpNetworkStream); }

            return smtpStreamWriter;
        }

    }

    SslStream GetSslStream()
    {
        if (sslstream == null) { 
            sslstream = new SslStream(smtpNetworkStream);
           sslstream.AuthenticateAsClient(strIpAddress, null, System.Security.Authentication.SslProtocols.Tls, false);

        }
        return sslstream;
    }

    public void DoConnect(string strIpAddress, int portno,bool EnableSSL) 
    {
        this.strIpAddress = strIpAddress;
        this.portNo = portno;
        try
        {
            UpdateStatus("trying to connect to " + strIpAddress + ":" + portno.ToString());
            smtpTcpClient = GetTcpClient();
            smtpTcpClient.Connect(strIpAddress, portno);

            if(EnableSSL==true)
            {
                this.EnableSSL = EnableSSL;
                smtpNetworkStream = smtpTcpClient.GetStream();

               //sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("EHLO [email protected] \n"));
               //sslstream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("STARTTLS \n"));

               smtpStreamReader = GetReadStream();//new StreamReader(new System.Net.Security.SslStream(smtpTcpClient.GetStream(), true,CertificateValidationCallback));
               //readerSmtp = new StreamReader(sslstream);
            }
            else
            {
                EnableSSL = false;
                smtpNetworkStream = smtpTcpClient.GetStream();
                smtpStreamReader = GetReadStream();
             }

            UpdateStatus(readResponse());

        }

        catch (Exception e)
        {
            //MessageBox.Show(e.Message);
            UpdateStatus("****CONNECTION ERROR****" + "\n" + e.Message);
            smtpTcpClient.Close();
        }

    }

Please help me..
Thanks so much in advance..

Edit: I know about the System.net.mail.smtpclient and other opensource smtp libraries available. But, I cannot use that to manually send smtp commands. This is not for sending e-mails. This is for seeing the email protocol work step by step through SSL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-10-29 16:21:06

You should probably just use System.Net.Mail.SMTPClient as this is what it is designed to be used with

り繁华旳梦境 2024-10-29 16:21:06

要与 TcpClient 连接,您必须先扫描 DNS,smtp.gmail.com 不是正确的服务器,请尝试以下操作:

var tcpclnt = new TcpClient();
tcpclnt.Connect("gmail-smtp-in.l.google.com", 25);

To connect with TcpClient you have to scan DNS before, smtp.gmail.com is not the right server, try this:

var tcpclnt = new TcpClient();
tcpclnt.Connect("gmail-smtp-in.l.google.com", 25);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文