Reading Email Using Pop3 in C#

Reading Email using Pop3 in C#

I've successfully used OpenPop.NET to access emails via POP3.

How to read latest email using pop3 c#

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

read outlook email in C# using pop3 (in korean)

Hi and welcome to Stack Overflow.
As per my understanding, you are using OpenPop.NET library.

OpenPop.NET uses EncodingFinder class to find correct encoding. By default it supports only utf8 and ascii (at least reading library code at github).
According to this page:
http://hpop.sourceforge.net/exampleChangeCharacterSetMapping.php
you can add your encoding(s) to EncodingFinder.
In your case, all you have to do is:

static void Main(string[] args)
{
EncodingFinder.AddMapping("ks_c_5601-1987", Encoding.GetEncoding(949));
// rest of the application

Please note this will work only on .NET Framework, not in .NET Core, since the latter supports a really limited number of encodings (https://docs.microsoft.com/en-us/dotnet/api/system.text.encodinginfo.getencoding?view=netcore-3.1).

I do not have a Korean pop3 on which to test this solution, but I hope it will work. Good luck!

Edit after some search
It should be possible to work with Korean encoding in .NET Core also, it's just a little trickier:

static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
EncodingFinder.AddMapping("ks_c_5601-1987", Encoding.GetEncoding(949));
// rest of application

Give it a try, if you are working with .NET Core.

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

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

Reading pop3 mails using OpenPop.Net

Pop3 protocol doesn't allow to mark anything on server. All emails can be marked only locally on your machine. All email clients using Pop3 have its own mark as read logic and this is done based local client database. You do not need to worry about other client used by users. Just download emails and store messageid somewhere locally maybe some other additional info as well. Then based on that process only not existing in local database emails.



Related Topics



Leave a reply



Submit