Guest User

Untitled

a guest
Oct 17th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace test_leer_imap
  8. {
  9. public class ClassImap
  10. {
  11. static System.Net.Sockets.TcpClient tcpc = null;
  12. static System.Net.Security.SslStream ssl = null;
  13. static string username, password;
  14. static string path;
  15. static int bytes = -1;
  16. static byte[] buffer;
  17. static StringBuilder sb = new StringBuilder();
  18. static byte[] dummy;
  19. //static void Main(string[] args) //<- antigua entrada
  20. public void Main2()
  21. {
  22. try
  23. {
  24. // there should be no gap between the imap command and the
  25. // ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server
  26. // cannot check for rn because in case of larger response from server ex:read email message
  27. // there are lot of lines so r n appears at the end of each line
  28. //ssl.timeout sets the underlying tcp connections timeout if the read or write
  29. //time out exceeds then the undelying connection is closed
  30. tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
  31.  
  32. ssl = new System.Net.Security.SslStream(tcpc.GetStream());
  33. ssl.AuthenticateAsClient("imap.gmail.com");
  34. receiveResponse("");
  35.  
  36. //Console.WriteLine("username : "); //<- antiguas lineas de la version consola, omito siguientes
  37. //username = Console.ReadLine();
  38. username = "MIusuario";
  39. password = "complicada";
  40.  
  41. receiveResponse("$ LOGIN " + username + " " + password + " rn");//<- comandaos al servidor, entiendo yo
  42.  
  43. receiveResponse("$ LIST " + """" + " "*"" + "rn");
  44.  
  45. receiveResponse("$ SELECT INBOXrn");
  46.  
  47. receiveResponse("$ STATUS INBOX (MESSAGES)rn");
  48.  
  49. //Console.WriteLine("enter the email number to fetch :");
  50. //int number = int.Parse(Console.ReadLine()); //<- pedia el numero de mail
  51. int number = 1; //<- le pongo uno a martillo,
  52.  
  53. receiveResponse("$ FETCH " + number + " body[header]rn");//<- le pide cabeza de ese mail
  54. receiveResponse("$ FETCH " + number + " body[text]rn");//<- le pide cuerpo de ese mail
  55.  
  56. receiveResponse("$ LOGOUTrn"); //<- desconecta
  57. }
  58. catch (Exception ex)
  59. {
  60. //Console.WriteLine("error: " + ex.Message);
  61. Program.Form1.RichOut("error: " + ex.Message);
  62. }
  63. finally
  64. {
  65. if (ssl != null)
  66. {
  67. ssl.Close();
  68. ssl.Dispose();
  69. }
  70. if (tcpc != null)
  71. {
  72. tcpc.Close();
  73. }
  74. }
  75.  
  76.  
  77. //Console.ReadKey();
  78. }
  79. static void receiveResponse(string command)
  80. {
  81. Program.Form1.RichOut("Comando: " + command); //<- mi trace de comandos
  82. try
  83. {
  84. if (command != "")
  85. {
  86. if (tcpc.Connected)
  87. {
  88. dummy = Encoding.ASCII.GetBytes(command);//<-pasa estring a matriz de chars
  89. ssl.Write(dummy, 0, dummy.Length);
  90. Program.Form1.RichOut("DUMMYS:" + UTF8Encoding.ASCII.GetString(dummy));
  91. }
  92. else
  93. {
  94. throw new ApplicationException("TCP CONNECTION DISCONNECTED");
  95. }
  96. }
  97. Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
  98. ssl.ReadTimeout = 100000; // <- le subo el timeout, NO va
  99. Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
  100.  
  101. buffer = new byte[8003];
  102. for (int k = 0; k < 8003; k++) { buffer[k] = 0; } //<- borro previo, TAMPOCO
  103.  
  104. System.Threading.Thread.Sleep(1000);//<- le meto retardo antes de leer, TAMPOCO
  105.  
  106. //ssl.Flush(); //<- estaba en la version original, sin esto falla igualmente.
  107. bytes = ssl.Read(buffer, 0, 8000); ////////////// <<<<<<<<------------- AQUI FALLA, se corta inesperadamente, bastante repetible el fallo
  108. //bytes = letras leidas, y es cierto, a pesar del fallo, devuelve las leidas reales, que luego muestra.
  109. //buffer[20] = 0; //<- CON ESTO SIMULO EL FALLO
  110.  
  111. Program.Form1.RichOut("Convert.ToString(bytes) = " + Convert.ToString(bytes));
  112.  
  113. //for (int k = 0; k < bytes + 22; k++) { if (buffer[k] == 0) { buffer[k] = 32; }; }//<- incluso trato de filtrar los 0s
  114.  
  115. sb.Append(Encoding.ASCII.GetString(buffer));
  116.  
  117. //Program.Form1.RichOut("TEST 1:" + ASCIIEncoding.Unicode.GetString(buffer)); //<- me rompo cabeza si eran distintas codificaciones
  118. //Program.Form1.RichOut("TEST 2:" + UnicodeEncoding.Unicode.GetString(buffer));
  119. //Program.Form1.RichOut("TEST 3:" + UTF8Encoding.Unicode.GetString(buffer));
  120. //Program.Form1.RichOut("rnrnTEST 01:" + ASCIIEncoding.UTF7.GetString(buffer));//<- alguna HASTA TRADUCE AL CHINO,,,
  121. Program.Form1.RichOut("rnrnTEST 23:" + UTF8Encoding.ASCII.GetString(buffer));//<- esta parece aceptable
  122.  
  123.  
  124. //for (int k = 0; k < bytes+22; k++) { Program.Form1.RichOut(Convert.ToString(Convert.ToChar(buffer[k])) + " " + Convert.ToString(Convert.ToInt16(buffer[k])) ); }
  125. //display de cada byte del bufer. se detiene antes de leer el 0.
  126.  
  127. }
  128. catch (Exception ex)
  129. {
  130. //Program.Form1.RichOut("trace 13");
  131. throw new ApplicationException(ex.Message);
  132. }
  133. }
  134.  
  135. }
  136. }
  137. //del lado de Formulario1, solo hay:
  138. // un boton, que tira aqui.
  139. //un richedit.
  140. // y 3º public void RichOut(string text) { richTextBox1.AppendText(text + ".FIN" + "rn"); }//atajo para escribir en el rich
  141. //OBSERVESE EL ".FIN", lo pongo para confirmar que acaba el string al rich.
  142. //EN EL FALLO QUE DESCRIBO NO LLEGA AL "FIN", se lo come.
  143.  
  144. TEST 23:* STATUS "INBOX" (MESSAGES 4)
  145. $ OK Success
  146. Comando: $ FETCH 1 body[header]
  147. .FIN
  148. DUMMYS:$ FETCH 1 body[header]
  149. .FIN
  150. ReadTimeout = 100000.FIN
  151. ReadTimeout = 100000.FIN
  152. Convert.ToString(bytes) = 258.FIN
  153. /////COMENTO YO : nos dice que llega bloque de 258, el contador de notepad lo confirma
  154. //// sin enbargo se come el "FIN" trampa, y el salto linea entre "153833Comando:"
  155. //// deberia ser
  156. ////153833.FIN
  157. ////Comando:,,,,,
  158.  
  159. TEST 23:* 1 FETCH (BODY[HEADER] {4890}
  160. Delivered-To: smitpegamoide@gmail.com
  161. Received: by 2002:ac8:464b:0:0:0:0:0 with SMTP id f11-v6csp3127811qto; Sun, 30
  162. Sep 2018 13:15:17 -0700 (PDT)
  163. X-Received: by 2002:aed:3461:: with SMTP id
  164. w88-v6mr6153972qtd.290.153833Comando: $ FETCH 1 body[text]
  165. .FIN
  166. DUMMYS:$ FETCH 1 body[text]
  167. .FIN
  168. ReadTimeout = 100000.FIN
  169. ReadTimeout = 100000.FIN
  170. Convert.ToString(bytes) = 514.FIN
  171.  
  172. ////// y aqui nos la vuelve a jugar
  173. TEST 23:8169517; Sun, 30 Sep 2018 13:09:29 -0700 (PDT)
  174. ARC-Seal: i=1; a=rsa-sha256; t=1538338169; cv=none; d=google.com;
  175. s=arc-20160816;
  176. b=h+0iEwcNEeOlUDUbggJa1gvLGGt9BDgDXqXRUtOltC6fSDEOrrezruEbDOdbgZJKm+
  177. 9e0aV/zQuSmyFrkyGXkakaladivvqrqZNDBQxAPJUcVs1z8Tja53EGoKCDFWsOZioGaU
  178. 1zx4foCqyURG1Mo8bADbylp1hniRl1emrX5SjCO9LZQKrCu/vWswYPSWUUQllDQbDyhu
  179. WpWxpRc4E+MyL3+sazBl+iObJGpNFHumSqtZDCLBTQ1g43cg6dtPV7FpA3ORROblHb2T
  180. EeUYW9fWHwskMS0l17Wbtwlx1RxxqVSxsPrwGAEmgFHTcMZmCLD3/GFWMXKhhfgq4HEy uEjQ==
  181. ARC-Message-SignatuComando: $ LOGOUT
  182. .FIN
  183. DUMMYS:$ LOGOUT
  184. .FIN
  185. ReadTimeout = 100000.FIN
  186. ReadTimeout = 100000.FIN
  187. Convert.ToString(bytes) = 1401.FIN
  188.  
  189.  
  190. TEST 23:re: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;
  191. s=arc-20160816;
  192. h=to:from:subject:message-id:feedback-id:reply-to:date:mime-version
  193. :dkim-signature; bh=TZgQiv9ExEo0q4bnOwxynQr1eWj08FA7GJB9gMVFX4g=;
  194. b=O9f8UVgxogmErWdefm1+gEhXbiQyIuwTpL8Qb86PVHS3aBqXl+9CgMHHsLhZkJm2jB
  195. +ohXJ/jmVbVcHZCp+eqbFcUy4020PayR/EkCrbHajwlsQpED9lgptQMY8jUeqFNijRgZ
  196. fRpoam/OrXiPHYr3RDBj1lC9W7L7EqlxZVy/EnYwMDrr3R9XkqXZewPXt5Friig0kY0l
  197. ZkkDlTFhLe7tOwA8WUGn0pWfBkfGvOqMVGA9bWkNZKjkQ1xkALwHMBYypR/hnQSZSLRP
  198. 5p2iTgZxgGyj0caIqi85gz///akWoBOmRSufX+fqaqXAmTazCNe0QfStE+8gKvpKvVeS nuxw==
  199. ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com
  200. header.s=20161025 header.b=UYPXnDxv; spf=pass (google.com: domain of
  201. 3ai2xwxskag0rzzrwpnzxxfytejeplx-yzcpawjrzzrwp.nzx@scoutcamp.bounces.google.com
  202. designates 209.85.220.69 as permitted sender)
  203. smtp.mailfrom=3ai2xWxsKAG0RZZRWPNZXXfYTejePLX-YZcPaWjRZZRWP.NZX@scoutcamp.bounces.google.com;
  204. dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com
  205. Return-Path: <3ai2xWxsKAG0RZZRWPNZXXfYTejePLX-YZcPaWjRZZRWP.NZX@scoutcamp.bounces.google.com>
  206. Received: from mail-sor-f69.google.com (mail-sor-f69.google.com.
  207. [209.85.220.69]) by mx.google.com with SMTPS id
  208. q1-v6sor1635267qtq.79.2018.09.30.13.09.14 for <smitpegamoide@gmail.com>
  209. (Google Transport Security); Sun, 30 Sep 2018 13:09:29 -0700 (PDT)
  210. Received-SPF: pass (google.com
  211. ////// y aqui la remata, dice que llegan 1401, y si, pero deberia llegar muchisimo mas`introducir el código aquí`
Add Comment
Please, Sign In to add comment