使用C#,如何通过POP3从Gmail服务器检索电子邮件列表
您好,
我有一个使用 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 />";
当我调试它时,它显示它已连接,但在检索电子邮件时没有响应。
Hy,
I have an application that sends mails with Gmail SMTP server (smtp.gmail.com) using SSL.
Now I want to read the emails from that account, does anyone have any idea how can I make this programatically in C# and ASP.NET?
At this point I'm using this code:
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 />";
And when I debug it it's shows that it's connected but no response in retrieving emails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个开源项目(我参与)的 POP3 功能包含您需要的一切。包括安全通信支持和高级身份验证。
如果您确实想自己做一个,浏览源代码可能会节省您的开发时间。
POP3 features of this open source project (I'm involved in) contains everything you need. Including secure communications support & advanced authentication.
If you really want to do one yourself, browsing the source code will probably save you days of development.
我正在使用代码项目中出现的示例库链接文本
它有一个漂亮且干净的 api 可以与 pop3 一起使用。
I´m using this sample library that appeared at The Code Project link text
that has a nice and clean api to work with pop3.