Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POP3 receive email encoding C#

I use POP3 to receive email. But encoding error like, email's Subject "主题" turns to "涓婚". Chinese errors, strong text when the content of the text's language is English, no errors. Who can tell me, what should I do for it? The code below:

POP3 pop = new POP3();
pop.Connect("userName", "password", "pop.126.com", 110);//smtp.126.com
pop.DownloadMessages();
for (int i = 1; i < pop.Messages.Count; i++)
{
    Email email = new Email();
    Message msg = pop.Messages[i];
    email.From = msg.From;
    email.FromName = msg.FromName;
    email.Body = msg.HTMLBody;
    email.Title = msg.Subject;
}
like image 891
stenfan he Avatar asked Dec 06 '22 00:12

stenfan he


1 Answers

I'm not sure what POP3 library you are using, but it is clearly broken and there's nothing you can do to "fix" your code to make it work beyond switching to another POP3 library, such as my MailKit library which is the only library that correctly handles charsets in all cases (most will handle Latin1 ok, but completely fail for CJK charsets).

The reason that most clients break for anything outside of Latin1 (ISO-8859-1) is that most email libraries have parsers that only work on strings. In order to convert the message data from bytes into a string, they need to pick a System.Text.Encoding (and most pick ISO-8859-1). They assume that email messages follow the rules outlined in the various RFCs that restrict email headers to US-ASCII, but it is very common for clients to ignore these rules.

Unlike those other parsers, MailKit's email parser parses byte streams and so does not require charset conversion before it can start parsing a message. This allows the parser to properly handle mixed charsets in the headers and body.

like image 93
jstedfast Avatar answered Dec 08 '22 12:12

jstedfast