0

我试图以 Windows 形式阅读我的电子邮件。那就是它应该在电子邮件收件箱中显示我的第一条消息。首先我尝试使用我的雅虎帐户。代码工作正常,但它没有显示我的信息。刚刚得到这个结果:“+OK hello from popgate-0.8.0.450444 pop113.plus.mail.bf1.yahoo.com”。

这是我的代码:

using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;

namespace emailcheck
 {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            // create an instance of TcpClient

            TcpClient tcpclient = new TcpClient();

            // HOST NAME POP SERVER and gmail uses port number 995 for POP

            tcpclient.Connect("pop.mail.yahoo.com", 995);

            // This is Secure Stream // opened the connection between client and POP Server

            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());

            // authenticate as client  

            sslstream.AuthenticateAsClient("pop.mail.yahoo.com");

            //bool flag = sslstream.IsAuthenticated;   // check flag

            // Asssigned the writer to stream 

            System.IO.StreamWriter sw = new StreamWriter(sslstream);

            // Assigned reader to stream

            System.IO.StreamReader reader = new StreamReader(sslstream);

            // refer POP rfc command, there very few around 6-9 command

            sw.WriteLine("user@yahoo.com");

            // sent to server
            sw.Flush(); sw.WriteLine("password");

            sw.Flush();

            // RETR 1 will retrive your first email. it will read content of your first email

            sw.WriteLine("RETR 1");

            sw.Flush();
            // close the connection
            sw.WriteLine("Quit ");
            sw.Flush(); string str = string.Empty;
            string strTemp = string.Empty;
            while ((strTemp = reader.ReadLine()) != null)
            {
                // find the . character in line
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }


            richTextBox1.Text = str;
          //  textBox1.Text ="Congratulation.. ....!!! You read your first gmail email ";
        }

        catch (Exception ex)
        {
            richTextBox1.Text = ex.ToString();
        }
    }
}}

谁能告诉我如何显示消息?以及如何检查其他域邮件,如 gmail、hotmail 等,

4

1 回答 1

1

为什么在从流中收到电子邮件之前退出?服务器在 RETR 命令后立即返回电子邮件内容。

我运行了你的代码,它寻找经典的登录方案:

sw.WriteLine("USER user@gmail.com"); // Refer to RFC 1939

// sent to server
sw.Flush();
strTemp = reader.ReadLine();

sw.WriteLine("PASS password");

sw.Flush();
strTemp = reader.ReadLine();

您现在应该可以使用 ie。+好的。欢迎。现在你应该可以弹出消息了。

要获取消息内容(从您的代码):

sw.WriteLine("RETR 1");

sw.Flush();
strTemp = reader.ReadLine();
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
    if (strTemp == ".")
    {
        break;
    }
    if (strTemp.IndexOf("-ERR") != -1)
    {
        break;
    }
    str += strTemp;
}

完整代码

try
        {
            // create an instance of TcpClient
            string str = string.Empty;
            string strTemp = string.Empty;
            TcpClient tcpclient = new TcpClient();

            // HOST NAME POP SERVER and gmail uses port number 995 for POP

            tcpclient.Connect("pop.gmail.com", 995);

            // This is Secure Stream // opened the connection between client and POP Server

            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());

            // authenticate as client  

            sslstream.AuthenticateAsClient("pop.gmail.com");

            //bool flag = sslstream.IsAuthenticated;   // check flag

            // Asssigned the writer to stream 

            System.IO.StreamWriter sw = new StreamWriter(sslstream);

            // Assigned reader to stream

            System.IO.StreamReader reader = new StreamReader(sslstream);
            strTemp = reader.ReadLine();
            // refer POP rfc command, there very few around 6-9 command

            sw.WriteLine("USER user@gmail.com");

            // sent to server
            sw.Flush();
            strTemp = reader.ReadLine();

            sw.WriteLine("PASS password");

            sw.Flush();
            strTemp = reader.ReadLine();

            // RETR 1 will retrive your first email. it will read content of your first email

            sw.WriteLine("RETR 1");

            sw.Flush();
            strTemp = reader.ReadLine();
            while ((strTemp = reader.ReadLine()) != null)
            {
                // find the . character in line
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }
            // close the connection
            sw.WriteLine("Quit ");
            sw.Flush(); 
            while ((strTemp = reader.ReadLine()) != null)
            {
                // find the . character in line
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }

            //  textBox1.Text ="Congratulation.. ....!!! You read your first gmail email ";
        }

        catch (Exception ex)
        {

        }
于 2013-08-05T06:46:59.673 回答