IMAP响应有限的大小

时间:2012-07-11 16:18:29

标签: buffer imap

我正在开发一个电子邮件客户端,该客户端将连接到Gmail邮箱并检索特定的电子邮件。

现在我可以连接到我的邮箱并且可以检索部分电子邮件而不是全部邮件,无论我的缓冲区有多大,我的电子邮件只收到1400个字符,然后邮件正文的其余部分为空。 / p>

您可以在此链接中找到电子邮件正文的屏幕截图

http://www.elzouhery.com/Mail%20Snapshot.png

先谢谢

修改

见下面的完整代码

    static void Main(string[] args)
        {
            TcpIMAP imap = ConnectToEmail();
            Console.WriteLine("Total Messages " + imap.MailCount());
            Console.WriteLine("Total Unread Messages " + imap.MailUnreadCount());
            Console.WriteLine("******************************************************");
            imap.SelectInbox();

            StreamWriter writer = null;
            int mailCount = imap.MailCount();
            var mailSize = string.Empty;
            var content = string.Empty;
            var subject = string.Empty;


            for (int i = 1; i < mailCount; i++)
            {
                try
                {
                    writer = new StreamWriter(@"c:\Mails\" + i + ".txt", true);
                    content = imap.GetMessage(i).ToString();
                    writer.Write(content);
                    writer.Close();
                }
                catch(Exception ex)
                {
                    writer.Write(content);
                    Console.Write(ex.Message);
                    writer.Close();
                }
            }
        }

        private static TcpIMAP ConnectToEmail()
        {
            string host = "imap.gmail.com";
            string username = "************";
            string password = "************";

            TcpIMAP imap = new TcpIMAP();
            imap.Connect(host, 993);
            imap.AuthenticateUser(username, password);
            return imap;
        }

        public static string GetMailSubject(string Header)
        {
            var headerLines = Header.Split(Environment.NewLine.ToCharArray());
            foreach (var line in headerLines)
            {
                if (line.IndexOf("Subject") > -1)
                {
                    return line.Replace("Subject: ", "");
                }
            }
            return "";
        }
/***************************************************/
class TcpIMAP
{
    private TcpClient _imapClient;
    private Stream _imapNs;
    private StreamWriter _imapSw;
    private StreamReader _imapSr;

    public TcpIMAP()
    {

    }

    public TcpIMAP(string hostname, int port)
    {
        InitializeConnection(hostname, port);
    }

    public void Connect(string hostname, int port)
    {
        InitializeConnection(hostname, port);
    }

    private void InitializeConnection(string hostname, int port)
    {
        try
        {
            _imapClient = new TcpClient(hostname, port);
            System.Net.Security.SslStream sslstream = new System.Net.Security.SslStream(_imapClient.GetStream());
            sslstream.AuthenticateAsClient("imap.gmail.com");
            _imapNs = sslstream;
            _imapSw = new StreamWriter(_imapNs);
            _imapSr = new StreamReader(_imapNs);

            Console.WriteLine("*** Connected ***");
            Response();
        }
        catch (SocketException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }


    public void AuthenticateUser(string username, string password)
    {
        _imapSw.WriteLine("$ LOGIN " + username + " " + password);
        _imapSw.Flush();
        Response();
    }


    public int MailCount()
    {
        _imapSw.WriteLine("$ STATUS INBOX (messages)");
        _imapSw.Flush();

        string res = Response();
        Match m = Regex.Match(res, "[0-9]*[0-9]");
        return Convert.ToInt32(m.ToString());
    }

    public int MailUnreadCount()
    {
        _imapSw.WriteLine("$ STATUS INBOX (unseen)");
        _imapSw.Flush();

        string res = Response();
        Match m = Regex.Match(res, "[0-9]*[0-9]");
        return Convert.ToInt32(m.ToString());
    }


    public string SelectInbox()
    {
        _imapSw.WriteLine("$ SELECT INBOX");
        _imapSw.Flush();
        return Response();
    }


    public object GetMessageHeaders(int index)
    {
        _imapSw.WriteLine("$ FETCH " + index + " (body[header.fields (from subject date)])");
        _imapSw.Flush();

        return Response();
    }

    public object GetMessage(int index)
    {
        _imapSw.WriteLine("$ FETCH " + index + " BODY.PEEK[]");
        _imapSw.Flush();

        return Response();
    }
    private string Response()
    {
        byte[] data = new byte[_imapClient.ReceiveBufferSize];
        int ret = _imapNs.Read(data, 0, data.Length);
        string output = Encoding.ASCII.GetString(data).TrimEnd().Replace("\0", "");
        return output;
    }



    public void Disconnect()
    {
        _imapSw.WriteLine("$ LOGOUT");
        _imapSw.Flush();
        _imapClient.Close();
    }

    public string SendCommand(string command)
    {
        _imapSw.WriteLine("$ " + command);
        _imapSw.Flush();
        return Response();
    }

2 个答案:

答案 0 :(得分:0)

看起来您正在使用此处的代码或类似代码:

http://www.codeproject.com/Articles/29594/How-to-Access-Emails-Using-the-IMAP-Protocol

编写的代码错误,不适用于较大的邮件。 Response()调用需要循环调用.Read(),追加结果直到方法返回0(表示没有更多数据可用。)查看NetworkStream.Read的文档。

此外,使用IMAP库会更好(参见Accessing Imap in C#)。

答案 1 :(得分:0)

您只需更改接收缓冲区大小