Search code examples
c#pop3email-clientyahoo-mailopenpop

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


I want to read email from my yahoo mail account. I am using "OpenPop.Pop3" to read email from my yahoo mail account, I am using below code :-

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;
}

Same code is able to access other mails emails like gmail,outlook but while working with yahoo mail emails i am able to get Subject, Date but when came to message part that is:

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

Its give error "The stream used to retrieve responses from was closed".

Here is the "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()

Please let me know if i missing something or doing something wrong. Also I have make yahoo mail emails to be accessed anywhere using POP.


Solution

  • First, based on your code snippet, you are downloading each message 4 times. That's going to be super slow.

    As far as why you are getting the error, I do not know. I do not get an error using 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;
    }