如何TcpClient的类中使用SSL [英] How to use SSL in TcpClient class

查看:626
本文介绍了如何TcpClient的类中使用SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET框架中还有一类的TcpClient 来检索电子邮件服务器的电子邮件。该的TcpClient 类有4构造与内搭最多两个参数的服务器连接。它工作正常与不使用SSL的服务器。但是,Gmail或其他许多电子邮件提供商使用SSL IMAP。

In the .NET framework there is a class TcpClient to retrieve emails from an email server. The TcpClient class has 4 constructors to connect with the server which take at most two parameters. It works fine with those servers which does not use SSL. But gmail or many other email providers use SSL for IMAP.

我可以与Gmail服务器连接,但不能与email_id和密码进行身份验证。  我的$ C $下验证用户是

I can connect with gmail server but can not authenticate with email_id and password. My code for authenticate user is

   'public void AuthenticateUser(string username, string password)
    {
        _imapSw.WriteLine("$ LOGIN " + username + " " + password);
         //_imapSw is a object of StreamWriter class
        _imapSw.Flush();
        Response();
    }'

但是,这code不能登录。

But this code can not login.

那么,如何可以使用的TcpClient 类检索邮件时,我不得不使用SSL?

So how can I use the TcpClient class to retrieve emails when I have to use SSL?

推荐答案

您必须使用的 SslStream 随着 TcpClient的则使用SslStream来读取数据,而不是TcpClient的

You have to use the SslStream along with the TcpClient then use the SslStream to read the data rather than the TcpClient.

线沿线的东西:

        TcpClient mail = new TcpClient();
        SslStream sslStream;

        mail.Connect("pop.gmail.com", 995);
        sslStream = new SslStream(mail.GetStream());

        sslStream.AuthenticateAsClient("pop.gmail.com");

        byte[] buffer = new byte[2048];
        StringBuilder messageData = new StringBuilder();
        int bytes = -1;
        do
        {
            bytes = sslStream.Read(buffer, 0, buffer.Length);

            Decoder decoder = Encoding.UTF8.GetDecoder();
            char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
            decoder.GetChars(buffer, 0, bytes, chars, 0);
            messageData.Append(chars);

            if (messageData.ToString().IndexOf("<EOF>") != -1)
            {
                break;
            }
        } while (bytes != 0);

        Console.Write(messageData.ToString());
        Console.ReadKey();

修改

以上code将简单地通过SSL到Gmail和输出测试消息的内容进行连接。要登录到Gmail帐户,并发出命令,你需要做的线沿线的东西:

The above code will simply connect via SSL to Gmail and output the contents of a test message. To log in to a gmail account and issue commands you will need to do something along the lines of:

        TcpClient mail = new TcpClient();
        SslStream sslStream;
        int bytes = -1;

        mail.Connect("pop.gmail.com", 995);
        sslStream = new SslStream(mail.GetStream());

        sslStream.AuthenticateAsClient("pop.gmail.com");

        byte[] buffer = new byte[2048];
        // Read the stream to make sure we are connected
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));

        //Send the users login details
        sslStream.Write(Encoding.ASCII.GetBytes("USER USER_EMAIL\r\n"));
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));

        //Send the password                        
        sslStream.Write(Encoding.ASCII.GetBytes("PASS USER_PASSWORD\r\n"));
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));

        // Get the first email 
        sslStream.Write(Encoding.ASCII.GetBytes("RETR 1\r\n"));
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytes));

显然,如果没有code全部重复:)

Obviously, without all the duplication of code :)

这篇关于如何TcpClient的类中使用SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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