How to read latest email using pop3 c#

情到浓时终转凉″ 提交于 2019-12-10 14:53:49

问题


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

using OpenPop.Pop3;    
public DataTable ReadEmailsFromId()
        {
            DataTable table = new DataTable();
            try
            {
                using (Pop3Client client = new Pop3Client())
                {
                    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;
        }

But its fetching emails before 4 March 2016, Not fetching new/latest emails. Is there any restriction in "OpenPop.Pop3" or am I missing something?


回答1:


To get new emails just put up keyword "recent:" before username then it will give emails which has been received in the last 30 days.

 using OpenPop.Pop3;    
    public DataTable ReadEmailsFromId()
    {
        DataTable table = new DataTable();
        try
        {
            using (Pop3Client client = new Pop3Client())
            {
                client.Connect("pop.gmail.com", 995, true); //For SSL                
                client.Authenticate("recent: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;
    }



回答2:


Gmail over a POP3 has some quirks to it. Try these links and see if they help.

Get latest mail from the mail server using OpenPop

What non-standard behaviour features does Gmail exhibit, when it is programmatically used as a POP3 server?



来源:https://stackoverflow.com/questions/40153786/how-to-read-latest-email-using-pop3-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!