POP3 клиент quoted-printable декодирование - C#

Узнай цену своей работы

Формулировка задачи:

Никто не знает? Ну ладно. А как перекодировать из Quoted Printable в нормальный вид? Я использовал следующую конструкцию:
public static string GetDecodeString(string mimeString)
        {
            var regex = new Regex(@"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?=");
            var encodedString = mimeString;
            var decodedString = string.Empty;
 
            while (encodedString.Length > 0)
            {
                var match = regex.Match(encodedString);
                if (match.Success)
                {
                    // If the match isn't at the start of the string, copy the initial few chars to the output
                    decodedString += encodedString.Substring(0, match.Index);
 
                    var charset = match.Groups["charset"].Value;
                    var encoding = match.Groups["encoding"].Value.ToUpper();
                    var value = match.Groups["value"].Value;
 
                    if (encoding.Equals("B"))
                    {
                        // Encoded value is Base-64
                        var bytes = Convert.FromBase64String(value);
                        decodedString += Encoding.GetEncoding(charset).GetString(bytes);
                    }
                    else if (encoding.Equals("Q"))
                    {
                        // Encoded value is Quoted-Printable
                        // Parse looking for =XX where XX is hexadecimal
                        var regx = new Regex("(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase);
                        decodedString += regx.Replace(value, new MatchEvaluator(delegate(Match m)
                        {
                            byte[] bytes = new byte[m.Value.Length / 3];
 
                            for (int i = 0; i < bytes.Length; i++)
                            {
                                string hex = m.Value.Substring(i * 3 + 1, 2);
                                int iHex = Convert.ToInt32(hex, 16);
                                bytes[i] = Convert.ToByte(iHex);
                            }
                            return Encoding.GetEncoding(charset).GetString(bytes);
                        }));
                        decodedString = decodedString.Replace('_', ' ');
                    }
                    else
                    {
                        // Encoded value not known, return original string
                        // (Match should not be successful in this case, so this code may never get hit)
                        decodedString += encodedString;
                        break;
                    }
 
                    // Trim off up to and including the match, then we'll loop and try matching again.
                    encodedString = encodedString.Substring(match.Index + match.Length + 1);
                }
                else
                {
                    // No match, not encoded, return original string
                    decodedString += encodedString;
                    break;
                }
            }
            return decodedString;
        }
Вызывал и записывал в файл так:
using (StreamReader reader = new StreamReader(@"C:\" + filename+".txt"))
                    {
                        string str = RegexDecode.DecodeEncodedWordValue(reader.ReadToEnd());
                        string filenameDecode = @"TempMessageFileDecode" + i.ToString();
                        pop3.SaveToFile(str, @"C:\", filenameDecode);
                    }
А в файле с вложением отобразилось вот так:
Return-Path: <test@domain.local>
Received: from st41nb ([192.168.191.248])
by [192.168.191.248] (Courier Mail Server 2.10) with ESMTP id 00500001
for <csti@domain.local>; Mon, 15 Aug 2011 13:10:47 +0400
From: "Ilyasov" <test@domain.local>
To: <csti@domain.local>
Subject: FW: Калуга_газ_без расхода
Date: Mon, 15 Aug 2011 13:10:47 +0400
Message-ID: <000001cc5b2b$3867a660$a936f320$@local>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0001_01CC5B4C.BF794660"
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: AcxbEHnrAGcNB+GZTWKq+UW7nFwCZAAGreLw
Content-Language: ru
 
This is a multi-part message in MIME format.
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0002_01CC5B4C.BF794660"

------=_NextPart_001_0002_01CC5B4C.BF794660
Content-Type: text/plain;
charset="koi8-r"
Content-Transfer-Encoding: quoted-printable
 
=20
 
=20
 
From: removed@mail.ru [mailto:removed@mail.ru]=20
Sent: Monday, August 15, 2011 9:59 AM
To: removed@mail.ru
Subject: =EB=C1=CC=D5=C7=C1_=C7=C1=DA_=C2=C5=DA =D2=C1=D3=C8=CF=C4=C1
 
=20
 
=20

------=_NextPart_001_0002_01CC5B4C.BF794660
Content-Type: text/html;
charset="koi8-r"
Content-Transfer-Encoding: quoted-printable
 
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dkoi8-r">
<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:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta name=3DGenerator =
content=3D"Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
..MsoChpDefault
{mso-style-type:export-only;}
@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=3Dblue =
vlink=3Dpurple><div class=3DWordSection1><p class=3DMsoNormal><span =
style=3D'color:#1F497D'><o:p> </o:p></span></p><p =
class=3DMsoNormal><span =
style=3D'color:#1F497D'><o:p> </o:p></span></p><div =
style=3D'border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm =
0cm 0cm'><p class=3DMsoNormal><b><span =
style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span>=
</b><span style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"'> =
removed@mail.ru [mailto:removed@mail.ru] =
<br><b>Sent:</b> Monday, August 15, 2011 9:59 AM<br><b>To:</b> =
removed@mail.ru<br><b>Subject:</b> =
=EB=C1=CC=D5=C7=C1_=C7=C1=DA_=C2=C5=DA =
=D2=C1=D3=C8=CF=C4=C1<o:p></o:p></span></p></div><p =
class=3DMsoNormal><o:p> </o:p></p><p =
class=3DMsoNormal><o:p> </o:p></p></div></body></html>
------=_NextPart_001_0002_01CC5B4C.BF794660--
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: application/octet-stream;
name="310244_51401.110815"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="310244_51401.110815"
 
CP866_404
//51401:1108:310244:15:4::++
(20):::::::2479::::2479::
(2000003)::02 5211:706:20:::2479::::2479::
=3D=3D
##15.08.2011
##
##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
##
##
##
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: application/octet-stream;
name="310244_51401.110811"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="310244_51401.110811"
 
CP866_404
//51401:1108:310244:11:4::++
(20):::::::2479::::2479::
(2000003)::02 5211:706:20:::2479::::2479::
=3D=3D
##15.08.2011
##
##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
##
##
##
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: application/octet-stream;
name="310244_51401.110812"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="310244_51401.110812"
 
CP866_404
//51401:1108:310244:12:4::++
(20):::::::2479::::2479::
(2000003)::02 5211:706:20:::2479::::2479::
=3D=3D
##15.08.2011
##
##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
##
##
##
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: application/octet-stream;
name="310244_51401.110813"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="310244_51401.110813"
 
CP866_404
//51401:1108:310244:13:4::++
(20):::::::2479::::2479::
(2000003)::02 5211:706:20:::2479::::2479::
=3D=3D
##15.08.2011
##
##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
##
##
##
 
------=_NextPart_000_0001_01CC5B4C.BF794660
Content-Type: application/octet-stream;
name="310244_51401.110814"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="310244_51401.110814"
 
CP866_404
//51401:1108:310244:14:4::++
(20):::::::2479::::2479::
(2000003)::02 5211:706:20:::2479::::2479::
=3D=3D
##15.08.2011
##
##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
##
##
##
 
------=_NextPart_000_0001_01CC5B4C.BF794660--
Почему не декодировалось? Облазил все, что можно((( Подскажите, что делать - сроки поджимают.

Решение задачи: «POP3 клиент quoted-printable декодирование»

textual
Листинг программы
Attachment attachment = Attachment.CreateAttachmentFromString("", "=?utf-8?Q?=D0=9F=D0=B0=D0=BA=D0=B5=D1=82=5F=D0=BE=D1=82=D1=87=D0=B5=D1=82=D0=BD=D0=BE=D1=81=D1=82?=");
Console.WriteLine(attachment.Name);

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.231 из 5