如何使用pop3 c#阅读最新的电子邮件

时间:2016-10-20 11:58:01

标签: c# pop3 email-client openpop

我想从我的Gmail帐户中读取emial。 我正在使用" OpenPop.Pop3"要从我的Gmail帐户阅读电子邮件,我使用下面的代码: -

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

但它在2016年3月4日之前收到的电子邮件,没有提取新的/最新的电子邮件。 是否有限制在" OpenPop.Pop3"或者我错过了什么?

2 个答案:

答案 0 :(得分:6)

要获取新电子邮件,只需提供关键字"最近:"在用户名之前,它会给出过去30天内收到的电子邮件。

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

答案 1 :(得分:2)