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

发布于 2024-09-24 17:58:41 字数 1412 浏览 1 评论 0原文

您好,

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-10-01 17:58:41

这个开源项目(我参与)的 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.

情愿 2024-10-01 17:58:41

我正在使用代码项目中出现的示例库链接文本
它有一个漂亮且干净的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文