How to read latest email using pop3 c#
Asked Answered
C

2

5

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?

Croix answered 20/10, 2016 at 11:58 Comment(0)
B
6

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;
    }
Bedchamber answered 21/10, 2016 at 6:16 Comment(0)
L
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?

Longs answered 20/10, 2016 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.