简体   繁体   中英

How to encoding tcpclient stream in c#?

I have a problem with my pop3 client. i can't encoding in utf-8 or iso 8859-1 the emails that i retrieve from the tcpclient stream. This is the code:

TcpClient tcpClient = new TcpClient();
txtLog.Text = "Dico:\r\nConnect me to " + txtServer.Text + ":" + txtPort.Text + "\r\n\r\n";
tcpClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text));
NetworkStream netStream = tcpClient.GetStream();
System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream, iso_8859_1);

System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;


if (tcpClient.Connected)
{
    txtLog.Text = "Il server risponde:\r\n" + strReader.ReadLine() + "\r\n\r\n";
    byte[] WriteBuffer = new byte[100990024];
    ASCIIEncoding enc = new System.Text.ASCIIEncoding();

    WriteBuffer = enc.GetBytes("USER " + txtUser.Text + "\r\n");
    txtLog.Text = "Dico:\r\nHere's the username: " + txtUser.Text + "\r\n\r\n";
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    txtLog.Text = "Il server risponde\r\n" + strReader.ReadLine() + "\r\n\r\n";
    WriteBuffer = enc.GetBytes("PASS " + txtPass.Text + "\r\n");
    txtLog.Text = "Dico:\r\nHere's the password: " + txtPass.Text + "\r\n\r\n";
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    txtLog.Text = "Il server risponde\r\n" + strReader.ReadLine() + "\r\n\r\n";

    WriteBuffer = enc.GetBytes("RETR " + messaggio.Text + "\r\n");
    txtLog.Text = "Messaggio: " + messaggio.Text + "\r\n\r\n";
    string message;
    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
    while (true)
    {
        message = strReader.ReadLine();
        if (message == ".")
        {
            if (message == "/n") break;
            break;
        }
        else
        {
            txtLog.Text += message + "\r\n\r\n";
            continue;
        }
    }

I always get something like this "questa_=E8_una_prova_=F2_=EC_?=". What's wrong? Thank you.

You're looking at the transfer encoding, which is used to transfer the bytes that result from the text encoding.

While the text may be encoded with UTF-8 or any other character set, the transfer encoding is usually base 64 or quoted-printable . The string "questa_=E8_una_prova_=F2_=EC_?=" looks like the latter.

Take a look here on how to decode quoted-printable into a string, and see here for already existing POP3 client implementations in .NET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM