Guest User

Untitled

a guest
Oct 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using System;
  2.  
  3. ssl = new System.Net.Security.SslStream(tcpc.GetStream());
  4. ssl.AuthenticateAsClient("imap.gmail.com");
  5. receiveResponse("");
  6.  
  7. //Console.WriteLine("username : "); //<- antiguas lineas de la version consola, omito siguientes
  8. //username = Console.ReadLine();
  9. username = "MIusuario";
  10. password = "complicada";
  11.  
  12. receiveResponse("$ LOGIN " + username + " " + password + " rn");//<- comandaos al servidor, entiendo yo
  13.  
  14. receiveResponse("$ LIST " + """" + " "*"" + "rn");
  15.  
  16. receiveResponse("$ SELECT INBOXrn");
  17.  
  18. receiveResponse("$ STATUS INBOX (MESSAGES)rn");
  19.  
  20. //Console.WriteLine("enter the email number to fetch :");
  21. //int number = int.Parse(Console.ReadLine()); //<- pedia el numero de mail
  22. int number = 1; //<- le pongo uno a martillo,
  23.  
  24. receiveResponse("$ FETCH " + number + " body[header]rn");//<- le pide cabeza de ese mail
  25. receiveResponse("$ FETCH " + number + " body[text]rn");//<- le pide cuerpo de ese mail
  26.  
  27. receiveResponse("$ LOGOUTrn"); //<- desconecta
  28. }
  29. catch (Exception ex)
  30. {
  31. //Console.WriteLine("error: " + ex.Message);
  32. Program.Form1.RichOut("error: " + ex.Message);
  33. }
  34. finally
  35. {
  36. if (ssl != null)
  37. {
  38. ssl.Close();
  39. ssl.Dispose();
  40. }
  41. if (tcpc != null)
  42. {
  43. tcpc.Close();
  44. }
  45. }
  46.  
  47.  
  48. //Console.ReadKey();
  49. }
  50. static void receiveResponse(string command)
  51. {
  52. Program.Form1.RichOut("Comando: " + command); //<- mi trace de comandos
  53. try
  54. {
  55. if (command != "")
  56. {
  57. if (tcpc.Connected)
  58. {
  59. dummy = Encoding.ASCII.GetBytes(command);//<-pasa estring a matriz de chars
  60. ssl.Write(dummy, 0, dummy.Length);
  61. Program.Form1.RichOut("DUMMYS:" + UTF8Encoding.ASCII.GetString(dummy));
  62. }
  63. else
  64. {
  65. throw new ApplicationException("TCP CONNECTION DISCONNECTED");
  66. }
  67. }
  68. Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
  69. ssl.ReadTimeout = 100000; // <- le subo el timeout, NO va
  70. Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
  71.  
  72. buffer = new byte[8003];
  73. for (int k = 0; k < 8003; k++) { buffer[k] = 0; } //<- borro previo, TAMPOCO
  74.  
  75. System.Threading.Thread.Sleep(1000);//<- le meto retardo antes de leer, TAMPOCO
  76.  
  77. //ssl.Flush(); //<- estaba en la version original, sin esto falla igualmente.
  78. bytes = ssl.Read(buffer, 0, 8000); ////////////// <<<<<<<<------------- AQUI FALLA, se corta inesperadamente, bastante repetible el fallo
  79. //bytes = letras leidas, y es cierto, a pesar del fallo, devuelve las leidas reales, que luego muestra.
  80. //buffer[20] = 0; //<- CON ESTO SIMULO EL FALLO
  81.  
  82. Program.Form1.RichOut("Convert.ToString(bytes) = " + Convert.ToString(bytes));
  83.  
  84. //for (int k = 0; k < bytes + 22; k++) { if (buffer[k] == 0) { buffer[k] = 32; }; }//<- incluso trato de filtrar los 0s
  85.  
  86. sb.Append(Encoding.ASCII.GetString(buffer));
  87.  
  88. //Program.Form1.RichOut("TEST 1:" + ASCIIEncoding.Unicode.GetString(buffer)); //<- me rompo cabeza si eran distintas codificaciones
  89. //Program.Form1.RichOut("TEST 2:" + UnicodeEncoding.Unicode.GetString(buffer));
  90. //Program.Form1.RichOut("TEST 3:" + UTF8Encoding.Unicode.GetString(buffer));
  91. //Program.Form1.RichOut("rnrnTEST 01:" + ASCIIEncoding.UTF7.GetString(buffer));//<- alguna HASTA TRADUCE AL CHINO,,,
  92. Program.Form1.RichOut("rnrnTEST 23:" + UTF8Encoding.ASCII.GetString(buffer));//<- esta parece aceptable
  93.  
  94.  
  95. //for (int k = 0; k < bytes+22; k++) { Program.Form1.RichOut(Convert.ToString(Convert.ToChar(buffer[k])) + " " + Convert.ToString(Convert.ToInt16(buffer[k])) ); }
  96. //display de cada byte del bufer. se detiene antes de leer el 0.
  97.  
  98. }
  99. catch (Exception ex)
  100. {
  101. //Program.Form1.RichOut("trace 13");
  102. throw new ApplicationException(ex.Message);
  103. }
  104. }
  105.  
  106. }
Add Comment
Please, Sign In to add comment