如何使用 pop3 c# 从 Yahoo mail 中读取最新的电子邮件

How to read latest email from Yahoo mail using pop3 c#

我想阅读来自我的雅虎邮箱帐户的电子邮件。我正在使用 "OpenPop.Pop3" 从我的雅虎邮箱帐户读取电子邮件,我正在使用以下代码:-

using OpenPop.Pop3;    

public DataTable ReadEmailsFromId()
{
    DataTable table = new DataTable();
    try
    {
        using (Pop3Client client = new Pop3Client())
        {
            client.Connect("pop.mail.yahoo.com", 995, true); //For SSL                
            client.Authenticate("Username", "Password", AuthenticationMethod.UsernameAndPassword);

            int messageCount = client.GetMessageCount();
            for (int i = messageCount; i > 0; i--)
            {
                table.Rows.Add(client.GetMessage(i).Headers.Subject, client.GetMessage(i).Headers.DateSent);
                string msdId = client.GetMessage(i).Headers.MessageId;
                OpenPop.Mime.Message msg = client.GetMessage(i);
                OpenPop.Mime.MessagePart plainTextPart = msg.FindFirstPlainTextVersion();
                string message = plainTextPart.GetBodyAsText();                           
            }
        }
    }
return table;
}

相同的代码能够访问其他邮件电子邮件,如 gmail、outlook,但在使用雅虎邮件电子邮件时,我能够获得主题、日期,但是当到达消息部分时,即:

OpenPop.Mime.Message msg = client.GetMessage(i);
OpenPop.Mime.MessagePart plainTextPart = msg.FindFirstPlainTextVersion();

给出错误 "The stream used to retrieve responses from was closed"。

这里是 "StackTrace":

at OpenPop.Pop3.Pop3Client.IsOkResponse(String response)
at OpenPop.Pop3.Pop3Client.SendCommand(String command)
at OpenPop.Pop3.Pop3Client.Disconnect()
at OpenPop.Pop3.Pop3Client.Dispose(Boolean disposing)
at OpenPop.Pop3.Disposable.Dispose()

如果我遗漏了什么或做错了什么,请告诉我。 我还使雅虎邮箱电子邮件可以在任何地方使用 POP 访问。

首先,根据您的代码段,您将每条消息下载 4 次。那会非常慢。

至于为什么会出现错误,我不知道。使用 MailKit:

我没有收到错误
using MimeKit;
using MailKit;
using MailKit.Net.Pop3;    

public DataTable ReadEmailsFromId()
{
    DataTable table = new DataTable();
    try
    {
        using (Pop3Client client = new Pop3Client())
        {
            client.Connect("pop.mail.yahoo.com", 995, true); //For SSL                
            client.Authenticate("Username", "Password");

            for (int i = client.Count - 1; i >= 0; i--)
            {
                var msg = client.GetMessage (i);

                table.Rows.Add(msg.Subject, msg.Date);
                string msdId = msg.MessageId;
                string message = msg.TextBody;
            }
        }
    }
return table;
}