如何从pop3中读取UTF-8邮件附件 [英] How to read UTF-8 mail attachments from pop3

查看:215
本文介绍了如何从pop3中读取UTF-8邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

电子邮件包含UTF-8编码的XML文件附件. 我正在寻找一种从ASP.NET/Mono MVC4应用程序读取此附加内容的方法. 我尝试按照如何使用OpenPop保存电子邮件附件中所述使用openpop 使用代码

E-mail contains XML file attachment in UTF-8 encoding. I'm looking for a way to read this attahcmed from ASP.NET/Mono MVC4 application. I tried to use openpop as described in How to save 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();

结果是将带有xml字符串重音符号的字符转换为tow ??分数. 以下消息中的XXXLTEC O=C3=9C在xml变量中显示为XXXLTEC O??. 正确的结果是XXXLTEC OÜ

In result xml string accented characters are converted to tow ?? marks. XXXLTEC O=C3=9C in message below appears as XXXLTEC O?? in xml variable. Correct result is XXXLTEC OÜ

如何正确阅读UTF-8附件? 我尚未在OpenPop中找到任何选项来正确转换它.

How to read UTF-8 attachment properly ? I havent found any option in OpenPop to convert it correctly.

XML附件显示为

------=_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>

推荐答案

使用OpenPOP.NET可能无法解决此问题,因此,唯一可行的选择是使用另一个POP3库,例如 MailKit ,它没有此问题.

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

问题在于OpenPOP假定字符集为US-ASCII,因为Content-Type头中没有字符集参数,并且错误地强制文本使用该字符集编码进行转换(而不是接受的字符集是自由的).

The problem is that OpenPOP assumes that the charset is US-ASCII because there is no charset parameter in the Content-Type header and it wrongly forces text to convert using that charset encoding (instead of being liberal in what it accepts).

MailKit使用字符集后备逻辑来尝试确定它是哪个字符集.但是即使默认情况下它会出错(即TextPart.Text属性,您仍然可以使用TextPart.GetText (System.Text.Encoding encoding)覆盖内容).

MailKit, on the other hand, uses charset fallback logic to try and determine which charset it is. But even if it gets it wrong by default (i.e. the TextPart.Text property, you can still use TextPart.GetText (System.Text.Encoding encoding) to override things).

这篇关于如何从pop3中读取UTF-8邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆