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

时间:2011-03-16 15:07:58

标签: c# smtp gmail

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

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

如果我使用任何其他端口号(如25或587),则会因&#34而失败;由于意外的数据包格式,握手失败。"

这是我的代码..

 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 something123@gmail.com \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和其他可用的opensource smtp库。但是,我无法使用它来手动发送smtp命令。这不是用于发送电子邮件。这是为了通过SSL逐步查看电子邮件协议。

2 个答案:

答案 0 :(得分:1)

您应该只使用System.Net.Mail.SMTPClient,因为这是与

一起使用的设计

答案 1 :(得分:-1)

要连接TcpClient,您必须先扫描DNS,smtp.gmail.com不是正确的服务器,试试这个:

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