0

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

现在我可以连接到我的邮箱,并且可以检索部分电子邮件而不是全部,无论我的缓冲区有多大,我仍然只能从我的电子邮件中获得 1400 个字符,然后在邮件正文的其余部分获得 Null。

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

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();
    }
4

2 回答 2

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#)。

于 2012-07-12T13:53:34.063 回答
0

你只需要改变你的接收缓冲区大小

于 2017-04-20T05:16:21.203 回答