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

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

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

Никто не знает? Ну ладно. А как перекодировать из Quoted Printable в нормальный вид? Я использовал следующую конструкцию:
Листинг программы
  1. public static string GetDecodeString(string mimeString)
  2. {
  3. var regex = new Regex(@"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?=");
  4. var encodedString = mimeString;
  5. var decodedString = string.Empty;
  6. while (encodedString.Length > 0)
  7. {
  8. var match = regex.Match(encodedString);
  9. if (match.Success)
  10. {
  11. // If the match isn't at the start of the string, copy the initial few chars to the output
  12. decodedString += encodedString.Substring(0, match.Index);
  13. var charset = match.Groups["charset"].Value;
  14. var encoding = match.Groups["encoding"].Value.ToUpper();
  15. var value = match.Groups["value"].Value;
  16. if (encoding.Equals("B"))
  17. {
  18. // Encoded value is Base-64
  19. var bytes = Convert.FromBase64String(value);
  20. decodedString += Encoding.GetEncoding(charset).GetString(bytes);
  21. }
  22. else if (encoding.Equals("Q"))
  23. {
  24. // Encoded value is Quoted-Printable
  25. // Parse looking for =XX where XX is hexadecimal
  26. var regx = new Regex("(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase);
  27. decodedString += regx.Replace(value, new MatchEvaluator(delegate(Match m)
  28. {
  29. byte[] bytes = new byte[m.Value.Length / 3];
  30. for (int i = 0; i < bytes.Length; i++)
  31. {
  32. string hex = m.Value.Substring(i * 3 + 1, 2);
  33. int iHex = Convert.ToInt32(hex, 16);
  34. bytes[i] = Convert.ToByte(iHex);
  35. }
  36. return Encoding.GetEncoding(charset).GetString(bytes);
  37. }));
  38. decodedString = decodedString.Replace('_', ' ');
  39. }
  40. else
  41. {
  42. // Encoded value not known, return original string
  43. // (Match should not be successful in this case, so this code may never get hit)
  44. decodedString += encodedString;
  45. break;
  46. }
  47. // Trim off up to and including the match, then we'll loop and try matching again.
  48. encodedString = encodedString.Substring(match.Index + match.Length + 1);
  49. }
  50. else
  51. {
  52. // No match, not encoded, return original string
  53. decodedString += encodedString;
  54. break;
  55. }
  56. }
  57. return decodedString;
  58. }
Вызывал и записывал в файл так:
Листинг программы
  1. using (StreamReader reader = new StreamReader(@"C:\" + filename+".txt"))
  2. {
  3. string str = RegexDecode.DecodeEncodedWordValue(reader.ReadToEnd());
  4. string filenameDecode = @"TempMessageFileDecode" + i.ToString();
  5. pop3.SaveToFile(str, @"C:\", filenameDecode);
  6. }
А в файле с вложением отобразилось вот так:
Листинг программы
  1. Return-Path: <test@domain.local>
  2. Received: from st41nb ([192.168.191.248])
  3. by [192.168.191.248] (Courier Mail Server 2.10) with ESMTP id 00500001
  4. for <csti@domain.local>; Mon, 15 Aug 2011 13:10:47 +0400
  5. From: "Ilyasov" <test@domain.local>
  6. To: <csti@domain.local>
  7. Subject: FW: Калуга_газ_без расхода
  8. Date: Mon, 15 Aug 2011 13:10:47 +0400
  9. Message-ID: <000001cc5b2b$3867a660$a936f320$@local>
  10. MIME-Version: 1.0
  11. Content-Type: multipart/mixed;
  12. boundary="----=_NextPart_000_0001_01CC5B4C.BF794660"
  13. X-Mailer: Microsoft Office Outlook 12.0
  14. Thread-Index: AcxbEHnrAGcNB+GZTWKq+UW7nFwCZAAGreLw
  15. Content-Language: ru
  16. This is a multi-part message in MIME format.
  17. ------=_NextPart_000_0001_01CC5B4C.BF794660
  18. Content-Type: multipart/alternative;
  19. boundary="----=_NextPart_001_0002_01CC5B4C.BF794660"
  20.  
  21. ------=_NextPart_001_0002_01CC5B4C.BF794660
  22. Content-Type: text/plain;
  23. charset="koi8-r"
  24. Content-Transfer-Encoding: quoted-printable
  25. =20
  26. =20
  27. From: removed@mail.ru [mailto:removed@mail.ru]=20
  28. Sent: Monday, August 15, 2011 9:59 AM
  29. To: removed@mail.ru
  30. Subject: =EB=C1=CC=D5=C7=C1_=C7=C1=DA_=C2=C5=DA =D2=C1=D3=C8=CF=C4=C1
  31. =20
  32. =20
  33.  
  34. ------=_NextPart_001_0002_01CC5B4C.BF794660
  35. Content-Type: text/html;
  36. charset="koi8-r"
  37. Content-Transfer-Encoding: quoted-printable
  38. <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
  39. charset=3Dkoi8-r">
  40. <html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
  41. xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
  42. xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
  43. xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
  44. xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta name=3DGenerator =
  45. content=3D"Microsoft Word 12 (filtered medium)"><style><!--
  46. /* Font Definitions */
  47. @font-face
  48. {font-family:Calibri;
  49. panose-1:2 15 5 2 2 2 4 3 2 4;}
  50. @font-face
  51. {font-family:Tahoma;
  52. panose-1:2 11 6 4 3 5 4 4 2 4;}
  53. /* Style Definitions */
  54. p.MsoNormal, li.MsoNormal, div.MsoNormal
  55. {margin:0cm;
  56. margin-bottom:.0001pt;
  57. font-size:11.0pt;
  58. font-family:"Calibri","sans-serif";}
  59. a:link, span.MsoHyperlink
  60. {mso-style-priority:99;
  61. color:blue;
  62. text-decoration:underline;}
  63. a:visited, span.MsoHyperlinkFollowed
  64. {mso-style-priority:99;
  65. color:purple;
  66. text-decoration:underline;}
  67. span.EmailStyle17
  68. {mso-style-type:personal-reply;
  69. font-family:"Calibri","sans-serif";
  70. color:#1F497D;}
  71. ..MsoChpDefault
  72. {mso-style-type:export-only;}
  73. @page WordSection1
  74. {size:612.0pt 792.0pt;
  75. margin:2.0cm 42.5pt 2.0cm 3.0cm;}
  76. div.WordSection1
  77. {page:WordSection1;}
  78. --></style><!--[if gte mso 9]><xml>
  79. <o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
  80. </xml><![endif]--><!--[if gte mso 9]><xml>
  81. <o:shapelayout v:ext=3D"edit">
  82. <o:idmap v:ext=3D"edit" data=3D"1" />
  83. </o:shapelayout></xml><![endif]--></head><body lang=3DRU link=3Dblue =
  84. vlink=3Dpurple><div class=3DWordSection1><p class=3DMsoNormal><span =
  85. style=3D'color:#1F497D'><o:p> </o:p></span></p><p =
  86. class=3DMsoNormal><span =
  87. style=3D'color:#1F497D'><o:p> </o:p></span></p><div =
  88. style=3D'border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm =
  89. 0cm 0cm'><p class=3DMsoNormal><b><span =
  90. style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span>=
  91. </b><span style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"'> =
  92. removed@mail.ru [mailto:removed@mail.ru] =
  93. <br><b>Sent:</b> Monday, August 15, 2011 9:59 AM<br><b>To:</b> =
  94. removed@mail.ru<br><b>Subject:</b> =
  95. =EB=C1=CC=D5=C7=C1_=C7=C1=DA_=C2=C5=DA =
  96. =D2=C1=D3=C8=CF=C4=C1<o:p></o:p></span></p></div><p =
  97. class=3DMsoNormal><o:p> </o:p></p><p =
  98. class=3DMsoNormal><o:p> </o:p></p></div></body></html>
  99. ------=_NextPart_001_0002_01CC5B4C.BF794660--
  100. ------=_NextPart_000_0001_01CC5B4C.BF794660
  101. Content-Type: application/octet-stream;
  102. name="310244_51401.110815"
  103. Content-Transfer-Encoding: quoted-printable
  104. Content-Disposition: attachment;
  105. filename="310244_51401.110815"
  106. CP866_404
  107. //51401:1108:310244:15:4::++
  108. (20):::::::2479::::2479::
  109. (2000003)::02 5211:706:20:::2479::::2479::
  110. =3D=3D
  111. ##15.08.2011
  112. ##
  113. ##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
  114. ##
  115. ##
  116. ##
  117. ------=_NextPart_000_0001_01CC5B4C.BF794660
  118. Content-Type: application/octet-stream;
  119. name="310244_51401.110811"
  120. Content-Transfer-Encoding: quoted-printable
  121. Content-Disposition: attachment;
  122. filename="310244_51401.110811"
  123. CP866_404
  124. //51401:1108:310244:11:4::++
  125. (20):::::::2479::::2479::
  126. (2000003)::02 5211:706:20:::2479::::2479::
  127. =3D=3D
  128. ##15.08.2011
  129. ##
  130. ##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
  131. ##
  132. ##
  133. ##
  134. ------=_NextPart_000_0001_01CC5B4C.BF794660
  135. Content-Type: application/octet-stream;
  136. name="310244_51401.110812"
  137. Content-Transfer-Encoding: quoted-printable
  138. Content-Disposition: attachment;
  139. filename="310244_51401.110812"
  140. CP866_404
  141. //51401:1108:310244:12:4::++
  142. (20):::::::2479::::2479::
  143. (2000003)::02 5211:706:20:::2479::::2479::
  144. =3D=3D
  145. ##15.08.2011
  146. ##
  147. ##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
  148. ##
  149. ##
  150. ##
  151. ------=_NextPart_000_0001_01CC5B4C.BF794660
  152. Content-Type: application/octet-stream;
  153. name="310244_51401.110813"
  154. Content-Transfer-Encoding: quoted-printable
  155. Content-Disposition: attachment;
  156. filename="310244_51401.110813"
  157. CP866_404
  158. //51401:1108:310244:13:4::++
  159. (20):::::::2479::::2479::
  160. (2000003)::02 5211:706:20:::2479::::2479::
  161. =3D=3D
  162. ##15.08.2011
  163. ##
  164. ##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
  165. ##
  166. ##
  167. ##
  168. ------=_NextPart_000_0001_01CC5B4C.BF794660
  169. Content-Type: application/octet-stream;
  170. name="310244_51401.110814"
  171. Content-Transfer-Encoding: quoted-printable
  172. Content-Disposition: attachment;
  173. filename="310244_51401.110814"
  174. CP866_404
  175. //51401:1108:310244:14:4::++
  176. (20):::::::2479::::2479::
  177. (2000003)::02 5211:706:20:::2479::::2479::
  178. =3D=3D
  179. ##15.08.2011
  180. ##
  181. ##=C2=E5=F0=F1=E8=FF ARM_Maker - 4.4.8
  182. ##
  183. ##
  184. ##
  185. ------=_NextPart_000_0001_01CC5B4C.BF794660--
Почему не декодировалось? Облазил все, что можно((( Подскажите, что делать - сроки поджимают.

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

textual
Листинг программы
  1. 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?=");
  2. Console.WriteLine(attachment.Name);

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


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

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

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

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут