Viewing source for recipe2114cs.aspx

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Text" %>
<%@ import Namespace="System.Net.Sockets" %>
<%@ import Namespace="System.Threading" %>
<script runat="server">

    void Button1_Click(Object sender, EventArgs e) {
         TcpClient tcpClient = new TcpClient();
         EmailContent.Text = "";
         EmailCount.Text = "";
         try{
             tcpClient.Connect(TextBox1.Text, 110);
             NetworkStream networkStream = tcpClient.GetStream();
    
             byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
             networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
    
             byte[] sendBytes = Encoding.ASCII.GetBytes("USER " + TextBox2.Text + "\r\n");
             networkStream.Write(sendBytes, 0, sendBytes.Length);
    
             networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
    
             sendBytes = Encoding.ASCII.GetBytes("PASS " + TextBox3.Text + "\r\n");
             networkStream.Write(sendBytes, 0, sendBytes.Length);
    
             networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
    
             sendBytes = Encoding.ASCII.GetBytes("STAT\r\n");
             networkStream.Write(sendBytes, 0, sendBytes.Length);
    
             networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
    
             string returnstring = Encoding.ASCII.GetString(bytes);
             string[] returnarray = returnstring.Split(new char[]{' '});
             string smessages = returnarray[1];
             string returnmessage;
    
             if(System.Convert.ToInt32(smessages) > 0 && System.Convert.ToInt32(TextBox4.Text) < System.Convert.ToInt32(smessages) + 1){
                 EmailCount.Text = "<b>Message " + TextBox4.Text + " of " + smessages + "</b>";
                 sendBytes = Encoding.ASCII.GetBytes("RETR " + TextBox4.Text + "\r\n");
                 networkStream.Write(sendBytes, 0, sendBytes.Length);
                 Thread.Sleep(500);
                 networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);
                 returnmessage = Regex.Replace(Encoding.ASCII.GetString(bytes),"\r\n", "<br/>");
                 EmailContent.Text = returnmessage;
             }
             else{
                 EmailContent.Text = "Could not find message";
             }
    
             sendBytes = Encoding.ASCII.GetBytes("QUIT\r\n");
             networkStream.Write(sendBytes, 0, sendBytes.Length);
             tcpClient.Close();
    
         }
         catch {
             EmailContent.Text = "Could not retrieve email or your inbox is empty.";
         }
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <table cellspacing="0" cellpadding="2" align="center" border="0">
                <tbody>
                    <tr>
                        <td>
                            <p align="right">
                                <font face="Trebuchet MS">Host: &nbsp; </font>
                            </p>
                        </td>
                        <td>
                            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
                            <font face="Trebuchet MS"></font></td>
                    </tr>
                    <tr>
                        <td>
                            <p align="right">
                                <font face="Trebuchet MS">User Name: &nbsp; </font>
                            </p>
                        </td>
                        <td>
                            <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
                            <font face="Trebuchet MS"></font></td>
                    </tr>
                    <tr>
                        <td>
                            <p align="right">
                                <font face="Trebuchet MS">User Password: &nbsp; </font>
                            </p>
                        </td>
                        <td>
                            <asp:TextBox id="TextBox3" runat="server" TextMode="Password"></asp:TextBox>
                            <font face="Trebuchet MS"></font></td>
                    </tr>
                    <tr>
                        <td>
                            <p align="right">
                                <font face="Trebuchet MS">Message Number: &nbsp; </font>
                            </p>
                        </td>
                        <td>
                            <asp:TextBox id="TextBox4" runat="server">1</asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div align="center">
                                <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Get Email"></asp:Button>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
        <p align="center">
            <asp:Label id="EmailCount" runat="server" Font-Names="Arial"></asp:Label>
        </p>
        <p>
            <asp:Literal id="EmailContent" runat="server"></asp:Literal>
        </p>
    </form>
</body>
</html>