收到的电子邮件破碎 [英] Broken received emails

查看:75
本文介绍了收到的电子邮件破碎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过简单的IMAP收到附件的电子邮件客户:

class Program
{
    static StreamWriter sw = null;
    static TcpClient tcpc = null;
    static SslStream ssl = null;
    static string path;
    static StringBuilder sb = new StringBuilder();
    static byte[] dummy;
    string username = "user";
    string password = "pass";

    static void Main(string[] args)
    {
        try
        {
            path = Environment.CurrentDirectory + "\\emailresponse.txt";

            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);

            using (sw = new System.IO.StreamWriter(System.IO.File.Create(path)))
            using (tcpc = new System.Net.Sockets.TcpClient("imap.server.com", 993))
            using (ssl = new System.Net.Security.SslStream(tcpc.GetStream()))
            {
                ssl.AuthenticateAsClient("imap.server.com");
                receiveResponse("");                    
                receiveResponse("$ LOGIN " + username + " " + password + "\r\n");
                Console.WriteLine("enter the email number to fetch :");
                int number = int.Parse(Console.ReadLine());
                receiveResponse("$ FETCH " + number + " body[header]\r\n");
                receiveResponse("$ FETCH " + number + " body[text]\r\n");
                receiveResponse("$ LOGOUT\r\n");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    static void receiveResponse(string command)
    {
        try
        {
            if (command != "")
            {
                if (tcpc.Connected)
                {
                    dummy = Encoding.Default.GetBytes(command);
                    ssl.Write(dummy, 0, dummy.Length);
                }
                else
                {
                    throw new ApplicationException("TCP CONNECTION DISCONNECTED");
                }
            }
            ssl.Flush();
            byte[] Buffer = new byte[1024*50];
            int bites = ssl.Read(Buffer, 0, Buffer.Length);
            byte[] buffer = new byte[bites];
            Array.Copy(Buffer, 0, buffer, 0, bites);
            sb.Append(Encoding.Default.GetString(buffer));
            sw.WriteLine(sb.ToString());
            sb = new StringBuilder();
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.ToString());
        }
    }
}


一切都很好,但如果电子邮件大小超过~19kb,无论缓冲区大小如何,都会裁剪内容。如何解决? 

我发现缓冲区大小  咬伤  是
16384位或更少。


Everything is alright, but if email size is more than ~19kb, content is cropped regardless of buffer size. How to fix it? 

I found that the buffer size bites is 16384 bits or less.

推荐答案

也许你应该执行
ssl.Read 循环,直到返回的数字为零。使用 AddRange 将每个部分附加到
列表< byte> 。当没有更多数据时,使用
ToArray 将列表转换为数组,然后获取字符串。

Maybe you should execute ssl.Read in a loop until the returned number is zero. Append each portion to a List<byte> using AddRange. When no more data, convert the list to array using ToArray, then obtain the string.


这篇关于收到的电子邮件破碎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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