Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read latest email using pop3 c#

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?

like image 780
Anand Systematix Avatar asked Oct 20 '16 11:10

Anand Systematix


People also ask

How do I log into POP3 email?

Select Internet Email and click on Next. Enter your name and email address at user information. Select POP3 for the account type and enter pop.mail.com as incoming server and smtp.mail.com as outgoing server. Enter your mail.com email address and password as log in credentials.

What is POP3 fetching?

What Are Fetch Emails? With Fetch Email (POP3), your Emails are kept in the server until the client asks for it. Your Email won't be in the server for very long since the client will usually ask for it after a few minutes.


1 Answers

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;
    }
like image 55
Nayan Katkani Avatar answered Sep 26 '22 14:09

Nayan Katkani