1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| protected void Button1_Click(object sender, EventArgs e)
{
// Create a TCP client for a TCP connection
TcpClient tcpClient = new TcpClient();
txtLog.Text = "DEMANDE :<br>Se connecter sur " + txtServer.Text + " port " + txtPort.Text + "<br><br>";
// Connect this TCP client to the server IP/name and port specified in the form
tcpClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text));
// Create a network stream to retrieve data from the TCP client
NetworkStream netStream = tcpClient.GetStream();
// We need a stream reader to be able to read the network stream
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream, Encoding.ASCII);
// If the connection was made successfully
if (tcpClient.Connected)
{
txtLog.Text += "REPONSE :<br>" + strReader.ReadLine() + "<br><br>";
// Buffer to which we're going to write the commands
byte[] WriteBuffer = new byte[1024];
// We're passing ASCII characters
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
// Pass the username to the server
WriteBuffer = enc.GetBytes("USER " + txtUser.Text + "\r\n");
txtLog.Text += "DEMANDE :<br>Here's the username: " + txtUser.Text + "<br><br>";
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
txtLog.Text += "REPONSE :<br>" + strReader.ReadLine() + "<br><br>";
// Pass the password to the server
WriteBuffer = enc.GetBytes("PASS " + txtPass.Text + "\r\n");
txtLog.Text += "DEMANDE :<br>Here's the password: " + txtPass.Text + "<br><br>";
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
txtLog.Text += "REPONSE :<br>" + strReader.ReadLine() + "<br><br>";
// Maintenant que l'on est connecté, on demande la liste des messages
WriteBuffer = enc.GetBytes("LIST\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
string ListMessage;
txtLog.Text += "<b>Liste des messages</b><br><br>";
// On récupère le nombre de messages
int nbrMessage = 2;
int i = 1;
while (true)
{
ListMessage = strReader.ReadLine();
if (ListMessage == ".")
{
// It's the last message so exit the loop and continue
break;
}
else
{
if (i == 1)
{
//Premier passage : on récupère le nombre de messages
nbrMessage = Convert.ToInt32(ListMessage.Substring(4, (ListMessage.IndexOf("m") - 5)).ToString());
txtLog.Text += "Nombre de messages : " + nbrMessage.ToString() + "<br><br>Numéro et taille : <br>";
i++;
continue;
}
else
{
// List the message
txtLog.Text += ListMessage + "<br>";
i++;
continue;
}
}
}
txtLog.Text += "<br><b>Affichage des messages</b><br>";
for (int k = 1; k <= nbrMessage; k++)
{
// Maintenant, afficher les messages
txtLog.Text += "<i>Message numéro " + k.ToString() + "</i><br>";
WriteBuffer = enc.GetBytes("RETR " + k.ToString() + " \r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
string MESSAGE="";
string From="";
string Sujet="";
string Contenu="";
string sTemp = strReader.ReadLine();
if (sTemp[0] != '-')
{
while (sTemp != ".")
{
MESSAGE += sTemp + "<br>";
sTemp = strReader.ReadLine();
if (sTemp.Length != 0)
{
if (sTemp.Length >= 5)
{
if (sTemp.Substring(0, 5) == "From:")
From = sTemp.Substring(5, sTemp.Length - 5);
}
if (sTemp.Length >= 8)
{
if (sTemp.Substring(0, 8) == "Subject:")
Sujet = sTemp.Substring(8, sTemp.Length - 8);
}
}
}
}
try { Contenu = MESSAGE.Substring(MESSAGE.IndexOf("Content-Type: text/html; charset=ISO-8859-1")); }
catch { }
Contenu = Contenu.Trim().Substring(0, Contenu.Length - 36);
Contenu = Contenu.Trim().Substring(43, Contenu.Length - 43); // Suppression de Content-Type: text/html; charset=ISO-8859-1 et du code a la fin
if (Contenu.IndexOf("Content-Transfer-Encoding: quoted-printable") > -1) Contenu = Contenu.Trim().Substring(Contenu.IndexOf("Content-Transfer-Encoding: quoted-printable") + 43, Contenu.Length - (Contenu.IndexOf("Content-Transfer-Encoding: quoted-printable") + 43));
txtLog.Text += "from : " + From + "<br>Sujet : " + Sujet + "<br>Corps : <input type=text value=\"" + Contenu.Replace("\"","'")+"\"><br>";
}
txtLog.Text += "DEMANDE :<br>Merci, déconnexion...<br><br>";
WriteBuffer = enc.GetBytes("QUIT\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
txtLog.Text += "REPONSE :<br>" + strReader.ReadLine();
tcpClient.Close();
}
else txtLog.Text = "Non connecté";
} |
Partager