繁体   English   中英

如何使用TcpClient和SslStream从hotmail帐户获取最新邮件

[英]How to get the latest mail from hotmail account using TcpClient and SslStream

我创建了一个Windows测试应用程序,其中连接到我的hotmail帐户并在其中检查未读邮件。当前通过我的应用程序,我从hotmail帐户中获取了最后一封邮件。

我如何获取最新邮件,以及是否可以使用SSLStream对象获取最新邮件的主题和正文。

我在这里发布我的代码。请帮助我解决这个问题。任何适当的帮助将不胜感激。写着“获取第一封邮件”的地方,我仅获取第一封邮件的总字节数。请帮助我来获得第一个电子邮件主题和正文。

        TcpClient mail = new TcpClient();
        SslStream sslStream;
        int bytes = -1;
        mail.Connect("pop3.live.com", 995);
        sslStream = new SslStream(mail.GetStream());
        sslStream.AuthenticateAsClient("pop3.live.com");

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

        //Send the users login details
        sslStream.Write(Encoding.ASCII.GetBytes("USER user_name\r\n"));
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        string message1 = Encoding.ASCII.GetString(buffer, 0, bytes);
        MessageBox.Show(message1);

        //Send the password                        
        sslStream.Write(Encoding.ASCII.GetBytes("PASS password\r\n"));
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        string message2 = Encoding.ASCII.GetString(buffer, 0, bytes);
        MessageBox.Show(message2);

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

        string str = string.Empty;
        string strTemp = string.Empty;
        StreamReader reader = new StreamReader(sslStream); 
        while ((strTemp = reader.ReadLine()) != null)
        {

            // find the . character in line
            if (strTemp == ".")
            {
                break;

            }
            if (strTemp.IndexOf("-ERR") != -1)
            {
                break;

            }
            str += strTemp;

        }
        MessageBox.Show(str);
    }

通过对代码进行一些修改,我可以访问hotmail帐户。但是,当我尝试访问AOl帐户时,使用相同的代码。我遇到了IO异常。有人可以帮助我如何使用此代码连接到AOL邮件系统。谢谢你的帮助。

我认为您必须实现pop协议才能获取pop服务器的电子邮件; 在您的情况下,hotmail。 您将在此处找到有关打开来自诸如hotmail / gmail之类的pop3服务器的邮件的好文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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