Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace test_leer_imap
- {
- public class ClassImap
- {
- static System.Net.Sockets.TcpClient tcpc = null;
- static System.Net.Security.SslStream ssl = null;
- static string username, password;
- static string path;
- static int bytes = -1;
- static byte[] buffer;
- static StringBuilder sb = new StringBuilder();
- static byte[] dummy;
- //static void Main(string[] args) //<- antigua entrada
- public void Main2()
- {
- try
- {
- // there should be no gap between the imap command and the
- // ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server
- // cannot check for rn because in case of larger response from server ex:read email message
- // there are lot of lines so r n appears at the end of each line
- //ssl.timeout sets the underlying tcp connections timeout if the read or write
- //time out exceeds then the undelying connection is closed
- tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
- ssl = new System.Net.Security.SslStream(tcpc.GetStream());
- ssl.AuthenticateAsClient("imap.gmail.com");
- receiveResponse("");
- //Console.WriteLine("username : "); //<- antiguas lineas de la version consola, omito siguientes
- //username = Console.ReadLine();
- username = "MIusuario";
- password = "complicada";
- receiveResponse("$ LOGIN " + username + " " + password + " rn");//<- comandaos al servidor, entiendo yo
- receiveResponse("$ LIST " + """" + " "*"" + "rn");
- receiveResponse("$ SELECT INBOXrn");
- receiveResponse("$ STATUS INBOX (MESSAGES)rn");
- //Console.WriteLine("enter the email number to fetch :");
- //int number = int.Parse(Console.ReadLine()); //<- pedia el numero de mail
- int number = 1; //<- le pongo uno a martillo,
- receiveResponse("$ FETCH " + number + " body[header]rn");//<- le pide cabeza de ese mail
- receiveResponse("$ FETCH " + number + " body[text]rn");//<- le pide cuerpo de ese mail
- receiveResponse("$ LOGOUTrn"); //<- desconecta
- }
- catch (Exception ex)
- {
- //Console.WriteLine("error: " + ex.Message);
- Program.Form1.RichOut("error: " + ex.Message);
- }
- finally
- {
- if (ssl != null)
- {
- ssl.Close();
- ssl.Dispose();
- }
- if (tcpc != null)
- {
- tcpc.Close();
- }
- }
- //Console.ReadKey();
- }
- static void receiveResponse(string command)
- {
- Program.Form1.RichOut("Comando: " + command); //<- mi trace de comandos
- try
- {
- if (command != "")
- {
- if (tcpc.Connected)
- {
- dummy = Encoding.ASCII.GetBytes(command);//<-pasa estring a matriz de chars
- ssl.Write(dummy, 0, dummy.Length);
- Program.Form1.RichOut("DUMMYS:" + UTF8Encoding.ASCII.GetString(dummy));
- }
- else
- {
- throw new ApplicationException("TCP CONNECTION DISCONNECTED");
- }
- }
- Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
- ssl.ReadTimeout = 100000; // <- le subo el timeout, NO va
- Program.Form1.RichOut("ReadTimeout = " + Convert.ToString(ssl.ReadTimeout));
- buffer = new byte[8003];
- for (int k = 0; k < 8003; k++) { buffer[k] = 0; } //<- borro previo, TAMPOCO
- System.Threading.Thread.Sleep(1000);//<- le meto retardo antes de leer, TAMPOCO
- //ssl.Flush(); //<- estaba en la version original, sin esto falla igualmente.
- bytes = ssl.Read(buffer, 0, 8000); ////////////// <<<<<<<<------------- AQUI FALLA, se corta inesperadamente, bastante repetible el fallo
- //bytes = letras leidas, y es cierto, a pesar del fallo, devuelve las leidas reales, que luego muestra.
- //buffer[20] = 0; //<- CON ESTO SIMULO EL FALLO
- Program.Form1.RichOut("Convert.ToString(bytes) = " + Convert.ToString(bytes));
- //for (int k = 0; k < bytes + 22; k++) { if (buffer[k] == 0) { buffer[k] = 32; }; }//<- incluso trato de filtrar los 0s
- sb.Append(Encoding.ASCII.GetString(buffer));
- //Program.Form1.RichOut("TEST 1:" + ASCIIEncoding.Unicode.GetString(buffer)); //<- me rompo cabeza si eran distintas codificaciones
- //Program.Form1.RichOut("TEST 2:" + UnicodeEncoding.Unicode.GetString(buffer));
- //Program.Form1.RichOut("TEST 3:" + UTF8Encoding.Unicode.GetString(buffer));
- //Program.Form1.RichOut("rnrnTEST 01:" + ASCIIEncoding.UTF7.GetString(buffer));//<- alguna HASTA TRADUCE AL CHINO,,,
- Program.Form1.RichOut("rnrnTEST 23:" + UTF8Encoding.ASCII.GetString(buffer));//<- esta parece aceptable
- //for (int k = 0; k < bytes+22; k++) { Program.Form1.RichOut(Convert.ToString(Convert.ToChar(buffer[k])) + " " + Convert.ToString(Convert.ToInt16(buffer[k])) ); }
- //display de cada byte del bufer. se detiene antes de leer el 0.
- }
- catch (Exception ex)
- {
- //Program.Form1.RichOut("trace 13");
- throw new ApplicationException(ex.Message);
- }
- }
- }
- }
- //del lado de Formulario1, solo hay:
- // un boton, que tira aqui.
- //un richedit.
- // y 3º public void RichOut(string text) { richTextBox1.AppendText(text + ".FIN" + "rn"); }//atajo para escribir en el rich
- //OBSERVESE EL ".FIN", lo pongo para confirmar que acaba el string al rich.
- //EN EL FALLO QUE DESCRIBO NO LLEGA AL "FIN", se lo come.
- TEST 23:* STATUS "INBOX" (MESSAGES 4)
- $ OK Success
- Comando: $ FETCH 1 body[header]
- .FIN
- DUMMYS:$ FETCH 1 body[header]
- .FIN
- ReadTimeout = 100000.FIN
- ReadTimeout = 100000.FIN
- Convert.ToString(bytes) = 258.FIN
- /////COMENTO YO : nos dice que llega bloque de 258, el contador de notepad lo confirma
- //// sin enbargo se come el "FIN" trampa, y el salto linea entre "153833Comando:"
- //// deberia ser
- ////153833.FIN
- ////Comando:,,,,,
- TEST 23:* 1 FETCH (BODY[HEADER] {4890}
- Delivered-To: smitpegamoide@gmail.com
- Received: by 2002:ac8:464b:0:0:0:0:0 with SMTP id f11-v6csp3127811qto; Sun, 30
- Sep 2018 13:15:17 -0700 (PDT)
- X-Received: by 2002:aed:3461:: with SMTP id
- w88-v6mr6153972qtd.290.153833Comando: $ FETCH 1 body[text]
- .FIN
- DUMMYS:$ FETCH 1 body[text]
- .FIN
- ReadTimeout = 100000.FIN
- ReadTimeout = 100000.FIN
- Convert.ToString(bytes) = 514.FIN
- ////// y aqui nos la vuelve a jugar
- TEST 23:8169517; Sun, 30 Sep 2018 13:09:29 -0700 (PDT)
- ARC-Seal: i=1; a=rsa-sha256; t=1538338169; cv=none; d=google.com;
- s=arc-20160816;
- b=h+0iEwcNEeOlUDUbggJa1gvLGGt9BDgDXqXRUtOltC6fSDEOrrezruEbDOdbgZJKm+
- 9e0aV/zQuSmyFrkyGXkakaladivvqrqZNDBQxAPJUcVs1z8Tja53EGoKCDFWsOZioGaU
- 1zx4foCqyURG1Mo8bADbylp1hniRl1emrX5SjCO9LZQKrCu/vWswYPSWUUQllDQbDyhu
- WpWxpRc4E+MyL3+sazBl+iObJGpNFHumSqtZDCLBTQ1g43cg6dtPV7FpA3ORROblHb2T
- EeUYW9fWHwskMS0l17Wbtwlx1RxxqVSxsPrwGAEmgFHTcMZmCLD3/GFWMXKhhfgq4HEy uEjQ==
- ARC-Message-SignatuComando: $ LOGOUT
- .FIN
- DUMMYS:$ LOGOUT
- .FIN
- ReadTimeout = 100000.FIN
- ReadTimeout = 100000.FIN
- Convert.ToString(bytes) = 1401.FIN
- TEST 23:re: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;
- s=arc-20160816;
- h=to:from:subject:message-id:feedback-id:reply-to:date:mime-version
- :dkim-signature; bh=TZgQiv9ExEo0q4bnOwxynQr1eWj08FA7GJB9gMVFX4g=;
- b=O9f8UVgxogmErWdefm1+gEhXbiQyIuwTpL8Qb86PVHS3aBqXl+9CgMHHsLhZkJm2jB
- +ohXJ/jmVbVcHZCp+eqbFcUy4020PayR/EkCrbHajwlsQpED9lgptQMY8jUeqFNijRgZ
- fRpoam/OrXiPHYr3RDBj1lC9W7L7EqlxZVy/EnYwMDrr3R9XkqXZewPXt5Friig0kY0l
- ZkkDlTFhLe7tOwA8WUGn0pWfBkfGvOqMVGA9bWkNZKjkQ1xkALwHMBYypR/hnQSZSLRP
- 5p2iTgZxgGyj0caIqi85gz///akWoBOmRSufX+fqaqXAmTazCNe0QfStE+8gKvpKvVeS nuxw==
- ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com
- header.s=20161025 header.b=UYPXnDxv; spf=pass (google.com: domain of
- 3ai2xwxskag0rzzrwpnzxxfytejeplx-yzcpawjrzzrwp.nzx@scoutcamp.bounces.google.com
- designates 209.85.220.69 as permitted sender)
- smtp.mailfrom=3ai2xWxsKAG0RZZRWPNZXXfYTejePLX-YZcPaWjRZZRWP.NZX@scoutcamp.bounces.google.com;
- dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com
- Return-Path: <3ai2xWxsKAG0RZZRWPNZXXfYTejePLX-YZcPaWjRZZRWP.NZX@scoutcamp.bounces.google.com>
- Received: from mail-sor-f69.google.com (mail-sor-f69.google.com.
- [209.85.220.69]) by mx.google.com with SMTPS id
- q1-v6sor1635267qtq.79.2018.09.30.13.09.14 for <smitpegamoide@gmail.com>
- (Google Transport Security); Sun, 30 Sep 2018 13:09:29 -0700 (PDT)
- Received-SPF: pass (google.com
- ////// 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