使用C#,如何通过POP3从Gmail服务器检索电子邮件列表

使用C#,如何通过POP3从Gmail服务器检索电子邮件列表,c#,asp.net,gmail,pop3,C#,Asp.net,Gmail,Pop3,Hy 我有一个应用程序,它使用SSL通过Gmail SMTP服务器(SMTP.Gmail.com)发送邮件 现在,我想阅读来自该帐户的电子邮件,有人知道我如何用C#和ASP.NET编程实现这一点吗 此时,我使用以下代码: TcpClient tcpClient = new TcpClient(); tcpClient.Connect("pop.gmail.com", 587); NetworkStream netStream = tcpClient.GetStream(); System.IO

Hy

我有一个应用程序,它使用SSL通过Gmail SMTP服务器(SMTP.Gmail.com)发送邮件

现在,我想阅读来自该帐户的电子邮件,有人知道我如何用C#和ASP.NET编程实现这一点吗

此时,我使用以下代码:

TcpClient tcpClient = new TcpClient();
tcpClient.Connect("pop.gmail.com", 587);

NetworkStream netStream = tcpClient.GetStream();
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);

Label7.Text = strReader.ReadLine() + "<br />"; 
//Label7.Text = "Server connected!";

byte[] WriteBuffer = new byte[1024];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();

WriteBuffer = enc.GetBytes("USER " + TextBox4.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("PASS " + TextBox5.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("LIST\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

String ListMessage;
while (true)
{
    ListMessage = strReader.ReadLine();
    if (ListMessage == ".")
    {
        break;
    }
    else
    {
        Label7.Text += ListMessage + "<br />";
        continue;
    }
}

WriteBuffer = enc.GetBytes("QUIT\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
Label7.Text += strReader.ReadLine() + "<br />"; 
TcpClient TcpClient=new TcpClient();
tcpClient.Connect(“pop.gmail.com”,587);
NetworkStream netStream=tcpClient.GetStream();
System.IO.StreamReader strReader=新的System.IO.StreamReader(netStream);
Label7.Text=strReader.ReadLine()+“
”; //Label7.Text=“服务器已连接!”; 字节[]WriteBuffer=新字节[1024]; ascienceoding enc=new System.Text.ascienceoding(); WriteBuffer=enc.GetBytes(“用户”+TextBox4.Text+”\r\n”); Write(WriteBuffer,0,WriteBuffer.Length); Label7.Text+=strReader.ReadLine()+“
”; WriteBuffer=enc.GetBytes(“传递”+TextBox5.Text+”\r\n); Write(WriteBuffer,0,WriteBuffer.Length); Label7.Text+=strReader.ReadLine()+“
”; WriteBuffer=enc.GetBytes(“列表\r\n”); Write(WriteBuffer,0,WriteBuffer.Length); 字符串列表消息; while(true) { ListMessage=strReader.ReadLine(); 如果(ListMessage==”) { 打破 } 其他的 { Label7.Text+=ListMessage+“
”; 继续; } } WriteBuffer=enc.GetBytes(“退出\r\n”); Write(WriteBuffer,0,WriteBuffer.Length); Label7.Text+=strReader.ReadLine()+“
”;

当我调试它时,它显示它已连接,但在检索电子邮件时没有响应。

我正在使用代码项目中出现的示例库 它有一个漂亮干净的api来处理pop3。

pop3的特性(我参与其中)包含了您所需要的一切。包括安全通信支持和高级身份验证


如果你真的想自己做一个,浏览源代码可能会为你节省几天的开发时间。

TcpClient.Connect地址中不应该是pop.gmail.com而不是smtp.gmail.com吗?啊,是的,我错了,我已经编辑到pop.gmail.com了,但我仍然有问题……这适用于任何类型的帐户?像gmail帐户吗?任何POP3帐户,包括gmail。这个项目在codeplex上已经过时了。。这些示例甚至没有生成。@Seany84:您必须引用以前编译的DLL,或者使用最新版本