How to read UTF-8 mail attachments from pop3

Email contains an UTF-8 encoded XML file attachment. I am looking for a way to read this using an ASP.NET/Mono MVC4 application. I tried using openpop as described in How to save an email attachment using OpenPop using code

using (OpenPop.Pop3.Pop3Client client = new Pop3Client()) { client.Connect("mail.company.com", 110, false); client.Authenticate("user", "pass", AuthenticationMethod.UsernameAndPassword); if (client.Connected) { int messageCount = client.GetMessageCount(); List<Message> allMessages = new List<Message>(messageCount); for (int i = messageCount; i > 0; i--) { var msg = client.GetMessage(i); var att = msg.FindAllAttachments(); foreach (var ado in att) { var xml = ado.GetBodyAsText(); 

As a result of xml, accented string characters are converted to tow? Tags. XXXLTEC O=C3=9C in the message below appears as XXXLTEC O?? in the xml variable. Correct result: XXXLTEC OÜ

How to read UTF-8? I did not find in OpenPop any option for the correct conversion.

The XML attachment in the message appears as

 ------=_NextPart_000_0066_01D0302C.83D6EFA0 Content-Type: text/xml; name="tapitolemas.xml" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="tapitolemas.xml" <?xml version=3D"1.0" encoding=3D"UTF-8"?> <E-Document> <Header> <DateIssued>2015-01-02T13:27</DateIssued> <SenderID>-</SenderID> <ReceiverID>1COL</ReceiverID> </Header> <Document> <DocumentType>invoice</DocumentType> <DocumentFunction>original</DocumentFunction> <DocumentParties> <BuyerParty context=3D"partner"> <PartyCode>1COL</PartyCode> <Name>XXXLTEC O=C3=9C</Name> 
0
source share
2 answers

There is probably no way around this using OpenPOP.NET, so your only real choice to get this to work is to use another POP3 library like MailKit that does not have this problem.

The problem is that OpenPOP assumes the encoding is US-ASCII, because there is no charset parameter in the Content-Type header, and this incorrectly converts the text using this encoding encoding (instead of being liberal in that she accepts).

MailKit, on the other hand, uses the backup charset logic to try to determine what encoding it is. But even if it is mistakenly mistaken (i.e. the TextPart.Text Property, you can still use TextPart.GetText (System.Text.Encoding encoding) to override things).

0
source

This can be fixed by changing the MessagePrt.cs GetBodyAsText () method to

  public string GetBodyAsText() { return Encoding.UTF8.GetString(Body); // Original gets ?? characters instead of unicode ones //return BodyEncoding.GetString(Body); } 
+1
source

All Articles