首页 > 解决方案 > 如何将 TIdMessage 从 koi8-r 解码为 windows-1251?

问题描述

我将 Embarcadero C++Builder 10.2 Tokyo 与 Indy 10 一起使用。

任务是通过 POP3 检查电子邮件。我可以收到电子邮件,但我不知道如何将他们的主题、附件文件名等从 koi8-r 解码为 windows-1251 或 UTF-8。

有谁能够帮我?

代码是:

int nMess = pop3->CheckMessages();
if (nMess == 0) return;
for (int i = 1; i <= nMess; i++) {
    pop3->UIDL(sl,i);   // check for UID
    UnicodeString sUID = sl->Strings[0];   // get one of them
    if (isLetterYetRead(sUID,_idBox)) continue;
    // if new, read it
    pop3->Retrieve(i,IdMessage1);
    int mSize = pop3->RetrieveMsgSize(i);
    // parsing
    for (int j = 0; j < IdMessage1->MessageParts->Count; j++) {
        TIdMessagePart *mp = IdMessage1->MessageParts->Items[j];
        // attacnets later
        if (mp->PartType() == mptAttachment) continue;
        TIdText *pt = (TIdText*) mp;
        if (pt->ContentType == "text/plain" )
            plain = pt->Body->Text;
        else if (pt->ContentType == "text/html" ) {
            html = pt->Body->Text;
            //  ??? bool g = NeedConvertToUnicode(IdMessage1->,pt->);
        }
    }
}

如何解码非英语字段?

标签: indypop3indy10

解决方案


电子邮件是:

`    Return-path: <babichev@meccanosoft.com>
     Envelope-to: ear@meccanosoft.com
     Delivery-date: Thu, 12 Jul 2018 14:00:33 +0300
     Received: from [213.79.95.226] (helo=pccomerc)
     by server120.hosting.reg.ru with esmtpa (Exim 4.90_1)
     (envelope-from <babichev@meccanosoft.com>)
     id 1fdZKr-0002FS-1c
     for ear@meccanosoft.com; Thu, 12 Jul 2018 14:00:33 +0300
     From: =?KOI8-R?B?4sHCyd7F1yDzxdLHxco=?= <babichev@meccanosoft.com>
     To: ear@meccanosoft.com
     Subject: test8
     Date: Thu, 12 Jul 2018 14:00:29 +0300
     Message-Id: <006901d419cf$8e2aeed0$aa80cc70$@meccanosoft.com>
     MIME-Version: 1.0
     Content-Type: multipart/mixed; boundary="UMwhv1yjwbAJnDKFK8wyCH0CYSe2=_553g"
     X-Mailer: Microsoft Outlook 16.0
     Thread-Index: AdQZz4RBq8MEckA1Rq6W+ide2hSKlw==
     Content-Language: ru
     In-Reply-To: <006901d419cf$8e2aeed0$aa80cc70$@meccanosoft.com>

     This is a multipart message in MIME format.

     --UMwhv1yjwbAJnDKFK8wyCH0CYSe2=_553g
     Content-Type: multipart/alternative;
     boundary="jEgWjeMCC35bF=_m8mr8EoHknZfRtWVKzB"

     --jEgWjeMCC35bF=_m8mr8EoHknZfRtWVKzB
     Content-Type: text/plain; charset="koi8-r"
     Content-Transfer-Encoding: 7bit
     Content-Disposition: inline

     TEST8


     --jEgWjeMCC35bF=_m8mr8EoHknZfRtWVKzB
     Content-Type: text/html; charset="koi8-r"
     Content-Transfer-Encoding: quoted-printable
     Content-Disposition: inline

     <html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas=
     -microsoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:off=
     ice:word" xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml"=
     xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta http-equiv=3DCo=
     ntent-Type content=3D"text/html; charset=3Dkoi8-r"><meta name=3DGenera=
     tor content=3D"Microsoft Word 15 (filtered medium)"><style><!--
     /* Font Definitions */
     @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
     @font-face
      {font-family:Calibri;
      panose-1:2 15 5 2 2 2 4 3 2 4;}
    /* Style Definitions */
    p.MsoNormal, li.MsoNormal, div.MsoNormal
      {margin:0cm;
      margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
     a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:#0563C1;
    text-decoration:underline;}
    a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:#954F72;
    text-decoration:underline;}
    span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri",sans-serif;
    color:windowtext;}
     =2EMsoChpDefault
      {mso-style-type:export-only;
      font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
    @page WordSection1
    {size:612.0pt 792.0pt;
    margin:2.0cm 42.5pt 2.0cm 3.0cm;}
    div.WordSection1
    {page:WordSection1;}
     --></style><!--[if gte mso 9]><xml>
    <o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
    </xml><![endif]--><!--[if gte mso 9]><xml>
    <o:shapelayout v:ext=3D"edit">
    <o:idmap v:ext=3D"edit" data=3D"1" />
    </o:shapelayout></xml><![endif]--></head><body lang=3DRU link=3D"#0563=
    C1" vlink=3D"#954F72"><div class=3DWordSection1><p class=3DMsoNormal><=
    span lang=3DEN-US>TEST8<o:p></o:p></span></p></div></body></html>

    --jEgWjeMCC35bF=_m8mr8EoHknZfRtWVKzB--

    --UMwhv1yjwbAJnDKFK8wyCH0CYSe2=_553g
    Content-Type: text/plain;
    name="=?KOI8-R?B?0M/e1MExLnR4dA==?="
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: attachment;
    filename="=?KOI8-R?B?0M/e1MExLnR4dA==?="
    ................................................
    --UMwhv1yjwbAJnDKFK8wyCH0CYSe2=_553g--
`

接收结果为:

收到 代码后的字母列表为:

     `  TIdPOP3 *pop3 = new TIdPOP3 ();
        TIdMessage *IdMessage1 = new TIdMessage();
        pop3->Host = qPop3->FieldByName("pop3server")->AsString;
        pop3->Port = qPop3->FieldByName("pop3port")->AsInteger;
        pop3->Username =  qPop3->FieldByName("pop3login")->AsString;
        pop3->Password = password;
        pop3->Connect();

        int nMess = pop3->CheckMessages();
        if (nMess == 0) return;
        for (int i = 1; i <= nMess; i++) {
           pop3->UIDL(sl,i);   // ask for UID
           UnicodeString sUID = sl->Strings[0]; 
           pop3->Retrieve(i,IdMessage1);
           int mSize = pop3->RetrieveMsgSize(i);
           UnicodeString s = L"c:\\Temp\\"+sUID+".eml";
           IdMessage1->SaveToFile(s,false);   
        // Parsing
           for (int j = 0; j < IdMessage1->MessageParts->Count; j++) {
               TIdMessagePart *mp = IdMessage1->MessageParts->Items[j];
       //   -------- and etc -------------
           }
       }
`

当然我做错了什么,但是什么?


推荐阅读